2012-04-21

Mumble (mumurd) nagios plugin

Today I wrote a little nagios plugin to check the state of murmurd (popular mumble server) because the ones out there don't seem to do the job (at least I couldn't make them).
requirements:
-dbus
-murmurd running with dbus interface
-python-dbus bindings
-nagios ;)

define a new command in /etc/nagios/objects/commands.cfg (at least under gentoo that's where to do it):

# murmur (Mumble)
define command{
        command_name check_murmur
        command_line $USER1$/check_murmur.py $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ 2>/dev/null
}


define the service in i.e. /etc/nagios/objects/localhost.cfg:

define service{
        use                 local-service
        host_name           example.org
        service_description Mumble murmurd
        check_command       check_murmur!servernum!minusers!maxusers!minchannels!maxchannels!minbans!maxbans
}


where servernum starts with 1
all minumums are disabled when <= 0
all maximums except bans are disabled when <= 0
maxbans is disabled when <= -1


now paste the script below in /usr/lib/nagios/plugins/check_murmur.py (you may have to put it in a different directory, but the filename should be the same).
Good luck and have fun :)

#!/usr/bin/python
import sys
import dbus


servernum = '1'
minusers = 0
maxusers = 0
minchannels = 0
maxchannels = 0
minbans = 0
maxbans = -1
warning = False
reportstr = ""
warningstr = ""


if(len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help'):
        print 'usage: ' + sys.argv[0] + ' <server number> <minusers> <maxusers> <minchannels> <maxchannels> <minbans> <maxbans>'
        print 'minimus: <= 0: disabled (except maxbans, only disabled for <= -1); maximums: <= 0 disabled; server numbers start with 1!'
        sys.exit()


servernum = sys.argv[1]
if(int(sys.argv[2]) > 0):
        minusers = int(sys.argv[2])
if(int(sys.argv[3]) > 0):
        maxusers = int(sys.argv[3])
if(int(sys.argv[4]) > 0):
        minchannels = int(sys.argv[4])
if(int(sys.argv[5]) > 0):
        maxchannels = int(sys.argv[5])
if(int(sys.argv[6]) > 0):
        minbans = int(sys.argv[6])
if(int(sys.argv[7]) > -1):
        maxbans = int(sys.argv[7])


bus = dbus.SystemBus()
server = bus.get_object('net.sourceforge.mumble.murmur', '/'+str(servernum))
try:
        players = server.getPlayers()
except dbus.exceptions.DBusException:
        print "Critical: No connection via dbus. If dbus is up this service is probably down."
        sys.exit(2) #Crit
channels = server.getChannels()
bans = server.getBans()




reportstr += "users: " + str(len(players)) + ", channels: " + str(len(channels)) + ", bans: " + str(len(bans))


if(minusers > 0):
        if(minusers > len(players)):
                warning = True
                warningstr += " <less than " + str(minusers) + " users>"
if(maxusers > 0):
        if(maxusers < len(players)):
                warning = True
                warningstr += " <more than " + str(maxusers) + " users>"
if(minchannels > 0):
        if(minchannels > len(channels)):
                warning = True
                warningstr += " <less than " + str(minchannels) + " channels>"
if(maxchannels > 0):
        if(maxchannels < len(channels)):
                warning = True
                warningstr += " <more than " + str(maxchannels) + " channels>"
if(minbans > 0):
        if(minbans > len(bans)):
                warning = True
                warningstr += " <less than " + str(minbans) + " bans>"
if(maxbans > -1):
        if(maxbans < len(bans)):
                warning = True
                warningstr +=" <more than " + str(maxbans) + " bans>"


if(warning):
        reportstr = "WARNING: " + reportstr + ", warnings:" + warningstr
else:
        reportstr = "OK: " + reportstr
print reportstr
if(warning):
        sys.exit(1)
else:
        sys.exit()


UPDATE: The plugin got accepted at nagios exchange.

2012-04-19

Prime numbers in Haskell

Hello again,
last night I couldn't sleep very well, so I tried a new method to calculate prime numbers(from 2 to n), this time in haskell.
I'm not exactly sure on the runtime, i guess O(n^2), maybe lower, but it's hard for me to say, because I believe it depends on the distribution of the prime numbers themselves. But I'm thankfull if anyone wants to analyze this and tell me :)


calcprimes :: Int -> [Int]
calcprimes n = primeh [2..n] []
 where primeh :: [Int] -> [Int] -> [Int]
primeh [] primes = reverse primes
primeh (x:xs) primes = primeh (primeh2 x xs [] ) (x:primes)
where primeh2 :: Int -> [Int] -> [Int] -> [Int]
primeh2 _ [] ys = reverse ys
primeh2 d (x:xs) ys = if mod x d == 0
                                      then primeh2 d xs ys
                                      else primeh2 d xs (x:ys)


UPDATE 1: Woops, it seems I accidentally reinvented (or remembered and just didn't know I saw this this before) the wheel, erm I mean the "Sieve of Eratosthenes" and also that there are shorter implementations of this, which can also handle infinite data structures and stuff - "much more to learn to I have..."


UPDATE 2: With a little lambda magic and "filter" I wrote a little shorter and even faster version of this:



calcprimes :: Int -> [Int]
calcprimes n = primeh [2..n] []
 where  primeh :: [Int] -> [Int] -> [Int]
        primeh [] primes = reverse primes
        primeh (x:xs) primes = primeh (filter (\y -> mod y x /= 0) xs) (x:primes)

2012-04-01

Mumble Server and SSL

Here we are again,

my friendly gentoo server and me, playing "wouldn't be fun without trouble" ;)
Goal:
- murmur as a Mumble server
- startcom / startssl class 1 certificate
Problem:

sslCert=/etc/ssl/startssl/server.pem
sslKey=/etc/ssl/startssl/server_key.pem
did not do the trick. Though it only throws error when you try to connect.
Then I tried different variants of cat-ing together cert and key, cert and intermediate authority, with ca itself, all of it, no luck at all.
Thanks to: http://www.mumb1e.de/de/gemeinde/forum/7-murmur/1151-ssl-zertifikat I tried the right thing:
sslCa=/etc/ssl/startssl/chain.pem
Where chain.pem just contains the ca and the intermediate cert of startcom. sslCert is only my cert and sslKey is only my key.
Hope I saved someone an hour with google ;)