Currently I write all my Sensu plugins in ruby. Even though I am not a ruby ninja, this has
really not been an issue as the plugins I write are typically fairly simple
(not to mention there are several already written plugins available see https://github.com/sensu/sensu-community-plugins/tree/master/plugins
[1] )
I do not recommend writing Sensu checks in any other language! I recommend keeping to Ruby…. But if you have to use another language you
can.
I did a few tests with a simple bash
script and a simple Scala Script.
Good Resources
I would suggest first going to this post and reading their
write up
Bash Script
When Sensu runs a check the
exit status of the check will tell Sensu how to handle it.
Exit Status
|
Sensu handles
as
|
0
|
OK
|
1
|
Warning
|
2
|
Critical
|
3 or more
|
Unknown
|
Now for some
experimenting. I am going to make a
simple bash script that does not output any text just exits. (I am going to use the code at http://www.programblings.com/2013/11/06/creating-a-sensu-check/
[2] … tweaking it a little.)
> sudo vi /etc/sensu/plugins/bash_test.sh
|
#!/bin/bash
rand=$RANDOM # 0 to 32,767
if [ $rand -gt 24000 ]; then
exit 0 # OK
elif [ $rand -gt 16000 ]; then
exit 1 #Warning
elif [ $rand -gt 8000 ]; then
exit 2 #Critical
else
exit 3 #Unknown
fi
|
Make it executable
> sudo chmod u+x /etc/sensu/plugins/bash_test.sh
|
My Sensu is run by a user named sensu so I need to chown on
this file
> sudo chown sensu:sensu /etc/sensu/plugins/bash_test.sh
|
Now test it.
> sudo ./bash_test.sh
|
If you run the command by hand you will not see anything.
To get the exit code of the last run command, run this
command
> echo $?
|
Slap the commands together
> sudo ./bash_test.sh;
echo $?
|
Looks like it's working.
Now add a simple check
> sudo vi
/etc/sensu/conf.d/check_bash_test.json
|
Here is my check (of course you will need to change the
subscribers to your own).
{
"checks":
{
"check_bash_test":
{
"command":
"/etc/sensu/plugins/bash_test.sh",
"interval":
60,
"occurrences":
1,
"subscribers":
[
"check-from-sensu-master"
]
}
}
}
|
Change the owner (I have a Sensu user)
> sudo chown sensu:sensu
/etc/sensu/conf.d/check_bash_test.json
|
Now restart all my services
> sudo service uchiwa stop && sudo service sensu-server stop && sudo service sensu-api stop && service sensu-client stop
> sudo service sensu-server start && sudo service sensu-api start && service sensu-client start && sudo service uchiwa start
|
Now tail the log
> sudo tail -f
/var/log/sensu/sensu-client.log | grep bash
|
There it is and it's unknown
= 3
Exit 1
Exit 2
It's working as expected.
When the status was 0 (OK) the check was resolved.
Now is 3 or more unknown?
Let m update the code to output 11 as a test.
> sudo vi /etc/sensu/plugins/bash_test.sh
|
#!/bin/bash
rand=$RANDOM # 0 to 32,767
if [ $rand -gt 24000 ]; then
exit 0 # OK
elif [ $rand -gt 16000 ]; then
exit 1 #Warning
elif [ $rand -gt 8000 ]; then
exit 2 #Critical
else
exit 11 #Unknown
fi
|
Yep 11 gets back unknown.
Adding in output
Let me edit the script one more time and add in text output
> sudo vi /etc/sensu/plugins/bash_test.sh
|
#!/bin/bash
rand=$RANDOM # 0 to 32,767
if [ $rand -gt 24000 ]; then
echo "It is OK"
exit 0 # OK
elif [ $rand -gt 16000 ]; then
echo "It is Warning"
exit 1 #Warning
elif [ $rand -gt 8000 ]; then
echo "It is Critical"
exit 2 #Critical
else
echo "It is Unknown"
exit 11 #Unknown
fi
|
From what I have seen you can also format the output like
this
<Class> <Status>: <Message>
|
#!/bin/bash
rand=$RANDOM #
0 to 32,767
if [ $rand -gt 24000 ];
then
echo "BASH_TEST OK: It is OK"
exit 0 #
OK
elif [ $rand -gt 16000 ];
then
echo "BASH_TEST WARNING: It is Warning"
exit 1 #Warning
elif [ $rand -gt 8000 ];
then
echo "BASH_TEST CRITICAL: It is Critical"
exit 2 #Critical
else
echo "BASH_TEST UNKNOWN: It is Unknown"
exit 11 #Unknown
fi
|
I don't see that this format is necessary but it is the
standard so why break it J
Write Check in Scala
What about writing a check in scala.
OK first I need to install Scala on my sensu box.
This is all running on an Ubuntu 14.04 server.
This is all running on an Ubuntu 14.04 server.
First Install Java 1.7
> sudo apt-get purge
openjdk*
> sudo
apt-get install python-software-properties
> sudo
add-apt-repository ppa:webupd8team/java
> sudo
apt-get update
> sudo
apt-get install oracle-java7-installer
> java
–version
|
Download scala
> wget http://downloads.typesafe.com/scala/2.11.7/scala-2.11.7.tgz
> tar xf
scala-2.11.7.tgz
|
> sudo mkdir
/usr/lib/scala
> sudo mv
scala-2.11.7 /usr/lib/scala/
> sudo touch
/usr/bin/scala
> sudo ln -fs
/usr/lib/scala/scala-2.11.7/bin/scala /usr/bin/scala
> sudo chmod
a+x /usr/bin/scala
> scala -version
|
Simple Scala Script
> sudo vi /etc/sensu/plugins/scala_test.sh
|
#!/usr/bin/env scala
import scala.util.Random;
val rand = new Random().nextInt(4)
rand match {
case 0 =>
println("SCALA_TEST OK: It is OK")
case 1 =>
println("SCALA_TEST WARNING: It is a Warning")
case 2 =>
println("SCALA_TEST CRITICAL: It is a Critical")
case _ =>
println("SCALA_TEST UNKNOWN: It is Unknown")
}
System.exit(rand)
|
Make it executable
> chmod u+x /etc/sensu/plugins/scala_test.sh
pr
|
My Sensu is run by a user named sensu so I need to chown on
this file
> sudo chown sensu:sensu /etc/sensu/plugins/scala_test.sh
|
Now test it.
> sudo ./scala_test.sh;
echo $?
|
Seems to be working… now to write a check and restart the
Sensu Services.
Now add a simple check
> sudo vi
/etc/sensu/conf.d/check_scala_test.json
|
Here is my check (of course you will need to change the
subscribers to your own).
{
"checks":
{
"check_scala_test":
{
"command":
"/etc/sensu/plugins/scala_test.sh",
"interval":
60,
"occurrences":
1,
"subscribers":
[
"check-from-sensu-master"
]
}
}
}
|
Change the owner (I have a Sensu user)
> sudo chown sensu:sensu
/etc/sensu/conf.d/check_scala_test.json
|
Now restart all my services
> sudo service uchiwa stop && sudo service sensu-server stop && sudo service sensu-api stop && service sensu-client stop
> sudo service sensu-server start && sudo service sensu-api start && service sensu-client start && sudo service uchiwa start
|
Now tail the log
> sudo tail -f
/var/log/sensu/sensu-client.log | grep scala
|
Hey it's working J
In conclusion… It's all about the exit codes J
References
[1] sensu-community-plugins (git repo)
Accessed 11/2015
[2] Creating a Sensu Check
Accessed 11/2015
No comments:
Post a Comment