Archive

Posts Tagged ‘tweet’

tweet nagios server status

August 28th, 2011 2 comments

On my Nagios Server I use twitter to send status alerts. It works nicely with Identica & Twitter.

The setup is easy. First install twidge with

root@host:~# apt-get install twidge

Then write a twidgerc file with the according twidge configuration

nagios@host:~# vi /etc/nagios3/twidgerc
[DEFAULT]
oauthaccesstoken: %(serverbase)s/oauth/access_token
oauthauthorize: %(serverbase)s/oauth/authorize
oauthdata: [("user_id","XXXXXX"),("screen_name","YOUR_SCREENNAME"),("oauth_verifier","XXXXXX"),("oauth_token","XXXXXX"),("oauth_token_secret","XXXXXX"),("oauth_callback_confirmed","true")]
oauthrequesttoken: %(serverbase)s/oauth/request_token
sendmail: /usr/sbin/sendmail
serverbase: https://api.twitter.com
shortenurls: yes
urlbase: %(serverbase)s/1

Make sure the file is readable by Nagios user. (!)

nagios@host:~# chown nagios /etc/nagios/twidgerc

Then add the following lines to /etc/nagios3/conf.d/contacts_nagios2.cfg (on GNU/Linux Debian).

define contact{
contact_name twitter
alias Twitter
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-twitter
host_notification_commands notify-host-by-twitter
email twitteraccount_to_contact
}

… and add “twitter” to the members in the contactgroup (in the same file).

members root,nagiosadmin,twitter

Then add these lines to etc/nagios3/commands.cfg:

define command {
command_name notify-service-by-twitter
command_line echo "#Nagios $NOTIFICATIONTYPE$ $HOSTNAME$($SERVICEDESC$) is $SERVICESTATE$" | twidge -c /etc/nagios3/twidgerc update
}
define command {
command_name notify-host-by-twitter
command_line echo "#Nagios $HOSTSTATE$ alert for $HOSTNAME$" | twidge -c /etc/nagios3/twidgerc dmsend $CONTACTEMAIL$
}

Nagios will tweet the service notifications and send a directmessage to the according user with the host notification.
Here’s an example:

=^.^= Lx

tweeting server status

March 11th, 2011 No comments

Since I’ve discovered twidge I find Twitter very useful, as my Server tweets me it’s status.
Therefore I wrote the following simple shell-script:

#!/bin/bash
#CC-BY-SA 11th of March 2011 by Lx
time=`date +"%d/%b/%Y:%H:%M:%S %z"`
if [ ! -e "~/log/$1_status.log" ]; then touch ~/log/$1_status.log; fi
lastlog_on=`tail -n 1 ~/log/$1_status.log | grep ONLINE`
lastlog_off=`tail -n 1 ~/log/$1_status.log | grep OFFLINE`
control=`ping -c 3 -W 1 google.com | grep "[1-3][[:space:]]\(packets received\|received\)"`
if [ -n $1 ]; then
 echo "Sending pings to "$1
 pong=`ping -c 3 -W 1 $1 | grep "[1-3][[:space:]]\(packets received\|received\)"`
 echo "Ping result: "$pong
 if [ -n "$pong" ]; then
  echo "Host "$1" is online"
  if [ -z "$lastlog_on" ]; then
   echo $time" "$1" status ONLINE" >> ~/log/$1_status.log
   echo "Host "$1" is online" | twidge dmsend twitteraccount
  fi
 elf [ -n "$control" ]; then
  echo "Host "$1" is offline"
  if [ -z "$lastlog_off" ]; then
   echo $time" "$1" status OFFLINE" >> ~/log/$1_status.log
   echo "Host "$1" is offline" | twidge dmsend twitteraccount
  fi
 else
  echo "Network down"
 fi
elif [ -z $1 ]; then
 echo "Usage: $arg0 hostname|ip"
fi
exit;

Then I use a cronjob accordingly:

*/5 * * * * sh ~/path/to/shellscript.sh foo.bar.baz > /dev/null 2>&1

You can easily also tweet the server uptime, load or whatever 😎

uptime | twidge update twitteraccount

Have phun!