Cacti (home)ForumsRepositoryDocumentation
Cacti: offical forums and support  

 FAQFAQ   SearchSearch   MemberlistMemberlist    RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in    


[HOWTO] Use a remote script via SSH as Data Input, no SNMP

 
Post new topic   Reply to topic    Cacti Forum Index -> Informational/HOWTO's
Author Message
white-jeroen



Joined: 22 Mar 2008
Posts: 9

PostPosted: Sun Mar 23, 2008 2:26 pm    Post subject: [HOWTO] Use a remote script via SSH as Data Input, no SNMP Reply with quote

I like to create scripts that process input for Cacti graphs on several servers, but I don't like to use SNMP to query them. I am using SSH to execute remote scripts for a year now, and I take the time to explain it here, so others can use it too.

Let say we have the following situation:

  • cactihost.your.dom = Server that runs cacti
  • queryme.your.dom = Server that you want to make a graph of
  • On cactihost.your.dom the Cacti cron job runs as user 'cacti'.
  • The Cacti home dir on cactihost.your.dom is /var/www/cacti

To make it possible for the Cacti cron job to execute a query script on queryme.your.dom, it needs to be able to log in using SSH without having to enter a password. We are going to realize this using a public key.

On cactihost.your.dom you become root and execute the following:
Code:
# mkdir /var/www/cacti/.ssh
# chown cacti:cacti /var/www/cacti/.ssh
# chmod 700 /var/www/cacti/.ssh
# su cacti
$ ssh-keygen
Enter file in which to save the key (/var/www/cacti/.ssh/id_rsa): [Return]
Enter passphrase (empty for no passphrase): [Return]
Enter same passphrase again: [Return]
$ cat /var/www/cacti/.ssh/id_rsa.pub

Copy the complete output of this last command.

Log in as root on queryme.your.dom and execute the following:
Code:
# adduser cactiquery
# su cactiquery
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ echo [Paste the copied text] >>~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys

Now let's test it. Execute the following on cactihost.your.dom.
I assume you are still user 'cacti', otherwise do # su cacti
Code:
$ ssh cactiquery@queryme.your.dom  -p22 'df -h'

The first time it ask if you are sure you want to continue, answer 'yes'.
The command should output the disk space status of the queryied server.
If you execute the command again, it should just output the info, and not ask anything.

So now let's create a test script that counts the number of httpd processes running on queryme.your.dom. Make sure you are logged in as 'cactiquery' on queryme.your.dom. Execute:
Code:
$ echo 'echo -n "httpds:"`ps h -C httpd |wc -l`' >~/httpds.sh
$ chmod 700 ~/httpds.sh
$ ~/httpds.sh

The last command executes the script, and shout output something like:
Quote:
httpds:8

Now lets do a last test on cactihost.your.dom, execute as user 'cacti':
Code:
$ ssh cactiquery@queryme.your.dom  -p22 '~/httpds.sh'

It should give the same output.

Now it's time to create the Data Input Method in the Cacti Console.
  • Click the 'Console' tab on the top of the cacti web interface
  • Click 'Data Input Methods'
  • Click 'Add' on the top right of the page
  • Name: httpd process count (ssh)
  • Input type: Script/Command
  • Input string:
    Code:
    ssh cactiquery@<hostname> -p22 '~/httpds.sh'

  • Click the create button
  • Click the 'Add' link in the Input Fields box
  • Field[Input]: hostname
  • Friendly name: Hostname
  • Special Type Code: hostname
  • Click 'create'
  • Click the 'Add' link in the Output Fields box
  • Field [Output]: httpds
  • Friendly Name: Number of httpd processes
  • Update RRD File: checked
  • Click 'create'
  • Click 'save'

It should look like this:


Now you can create a Data Template that uses this Data Input Method (as GAUGE), and a Graph Template using that Data Source.

I hope this howto helps someone to build his/her own remote query script and use it in Cacti, without the need of SMTP. Please let me know.


Last edited by white-jeroen on Tue Mar 25, 2008 8:58 am; edited 6 times in total
Back to top
white-jeroen



Joined: 22 Mar 2008
Posts: 9

PostPosted: Mon Mar 24, 2008 3:55 am    Post subject: If you followed this howto and your graph doesn't plot Reply with quote

Of course you need to wait at least 15 minutes before checking the results in the graph.
If your graph still doesn't plot anything:

  • In the Cacti web interface click the "Console" tab
  • Click 'System Utilities'
  • Click 'View Cacti Log File'
  • If you see yellow error lines, click on the DS[x] number

If it is the datasource you created yourself, there might be a problem with the Data Input Method

  • Click 'System Utilities'
  • Click 'View Poller Cache'
  • Search for the Data Source you created to connect to the Data Input method from the HowTo
  • Copy the line after 'Script:' from that item
  • Log in as root on cactihost.your.dom and:
    Code:
    # su cacti
    $ [paste the line you copied]


Maybe it asks again if you are sure you want to connect because the hostname is not exactly the same (for example an IP) as the hostname when you tested during the HowTo.
If that is the case, enter 'Yes' and test the command agian. Now it should not ask anything and from then your graph should start working.

If you get an other error, maybe that gives you an hint of what could be the problem.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Cacti Forum Index -> Informational/HOWTO's All times are GMT - 5 Hours
Page 1 of 1

 



Powered by phpBB © 2001, 2005 phpBB Group