Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
uploading the new code for hosts management on the Mconf network
Browse files Browse the repository at this point in the history
  • Loading branch information
fcecagno committed Sep 24, 2012
1 parent 9df1a0c commit ab8565d
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 17 deletions.
1 change: 1 addition & 0 deletions etc/nagios.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

cfg_file=/usr/local/nagios/etc/objects/mconf/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/mconf/hostgroups.cfg
cfg_file=/usr/local/nagios/etc/objects/mconf/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/mconf/services.cfg
cfg_file=/usr/local/nagios/etc/objects/mconf/commands.cfg
Expand Down
12 changes: 6 additions & 6 deletions etc/objects/mconf/commands.cfg
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
define command{
command_name check_dummy
command_line $USER1$/check_dummy $ARG1$ $ARG2$
command_name check_dummy
command_line $USER1$/check_dummy $ARG1$ $ARG2$
}

define command{
command_name add_nagios_host
command_line $USER1$/nagios-hosts/add-server $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $USER1$ $SERVICEOUTPUT$ >> /home/nagios/output 2>&1
command_name add_mconf_host
command_line $USER1$/mconf-hosts/add-server $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $USER1$ $SERVICEOUTPUT$ >> /var/log/add-server.log 2>&1
}

define command{
command_name get_bigbluebutton_info
command_line $USER1$/bigbluebutton/get-bigbluebutton-info.py --host $_HOSTBBB_URL$ --salt $_HOSTBBB_SALT$ # (removing the limits)--limits 5,10,5,5:10,20,10,10
command_name get_bigbluebutton_info
command_line $USER1$/bigbluebutton/get-bigbluebutton-info.py --host $_HOSTBBB_URL$ --salt $_HOSTBBB_SALT$ # (removing the limits)--limits 5,10,5,5:10,20,10,10
}

# command to process nagios performance data for nagiosgraph
Expand Down
26 changes: 26 additions & 0 deletions etc/objects/mconf/hostgroups.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define hostgroup {
hostgroup_name all-servers
hostgroup_members mconf-live-servers
alias All Servers
}

define hostgroup {
hostgroup_name mconf-live-servers
alias Mconf-Live servers
}

define hostgroup {
hostgroup_name temporary-servers
alias Temporary servers
}

define hostgroup {
hostgroup_name mconf-network-members
alias Mconf Network members
}

define hostgroup {
hostgroup_name partner-belnet
alias Servers from Belnet
}

9 changes: 0 additions & 9 deletions etc/objects/mconf/hosts.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
# Configuration file /home/felipe/codes/nagios-etc/etc/objects/mconf/hosts.cfg
# Edited by PyNag on Mon Feb 13 22:21:17 2012

define hostgroup {
hostgroup_name all-servers
members localhost
alias All servers
}

4 changes: 2 additions & 2 deletions etc/objects/mconf/services.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ define service{
host_name localhost
service_description Server UP
event_handler_enabled 1
event_handler add_nagios_host
event_handler add_mconf_host
notification_interval 0
check_freshness 0
}
Expand Down Expand Up @@ -54,6 +54,7 @@ define service{
check_interval 1 # min
check_period 24x7
active_checks_enabled 1
first_notification_delay 5
}

define service{
Expand Down Expand Up @@ -112,4 +113,3 @@ define service{
notification_interval 0
}


42 changes: 42 additions & 0 deletions libexec/mconf-hosts/add-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

echo "[`date +\"%Y/%m/%d %T\"`]"

SERVICE_STATE=$1
SERVICE_STATE_TYPE=$2
SERVICE_ATTEMPT=$3
USER_PATH=$4
echo "$SERVICE_STATE"
echo "$SERVICE_STATE_TYPE"
echo "$SERVICE_ATTEMPT"
echo "$USER_PATH"

if [ $SERVICE_STATE == "OK" ]
then
echo "State = OK, returning..."
/usr/bin/python $USER_PATH/mconf-hosts/mconf-hosts.py reload
exit 0
fi

MCONF_TYPE=$5
MCONF_HOST=$6
echo "type: $MCONF_TYPE"
echo "host: $MCONF_HOST"

if [ "$MCONF_TYPE" == "bigbluebutton" ] || [ "$MCONF_TYPE" == "mconf-live" ]
then
MCONF_SALT=$7
echo "salt: $MCONF_SALT"
PARAMS="$MCONF_HOST $MCONF_SALT"
else
PARAMS="$MCONF_HOST"
fi

/usr/bin/python $USER_PATH/mconf-hosts/mconf-hosts.py add $PARAMS

now=`date +%s`
commandfile="/usr/local/nagios/var/rw/nagios.cmd"
message="[%lu] PROCESS_SERVICE_CHECK_RESULT;localhost;Server UP;0;OK - Last server handled: $MCONF_HOST"
echo $message
/usr/bin/printf "$message\n" $now > $commandfile

220 changes: 220 additions & 0 deletions libexec/mconf-hosts/mconf-hosts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#!/usr/bin/python
# -*- coding: utf-8 -*

import os
import sys
import inspect
import json
import re
import pynag.Model
from pynag.Model import AttributeList
from pynag.Parsers import config
from urlparse import urlparse
from pprint import pprint

_DEBUG = False

if _DEBUG:
hosts_cfg = '../../etc/objects/mconf/hosts.cfg'
nagios_cfg = '../../etc/nagios.cfg'
else:
hosts_cfg = '/usr/local/nagios/etc/objects/mconf/hosts.cfg'
nagios_cfg = '/usr/local/nagios/etc/nagios.cfg'

hostgroups = [ 'mconf-live-servers', 'temporary-servers', 'mconf-network-members' ]

def print_usage():
print "Usage:"
print " add <SERVER_URL> <SALT>"
print " remove <HOSTNAME>"
print " detail <HOSTNAME>"
print " add-to-network <HOSTNAME>"
print " remove-from-network <HOSTNAME>"
print " list <%s>" % (' | '.join(hostgroups))
print " update-metadata <JSON_FILE>"
print " clean"
print " reload"
print " force-reload"
exit(0)

def reload(forced=False):
check_hosts();
last_modified = int(os.path.getmtime(hosts_cfg) * 1000)

# last_modified_filename = "updatehosts.tmp"
last_modified_filename = "/tmp/mconf-updatedhosts.tmp"
if os.path.exists(last_modified_filename):
last_modified_file = open(last_modified_filename, "r")
last_modified_stored = int(last_modified_file.readline())
last_modified_file.close()
else:
last_modified_stored = last_modified

if last_modified != last_modified_stored or forced:
print "Reloading Nagios configuration"
os.system("killall -HUP nagios")
else:
print "Unmodified content"

last_modified_file = open(last_modified_filename, "w")
last_modified_file.write("%d\n" % last_modified)
last_modified_file.close()

def add_host(url, salt):
url = url.lower()
address = urlparse(url).netloc
hostname = address.split(':')[0]
if not (hostname and len(hostname) > 0 and hostname != "localhost"):
print "==> Invalid hostname!"
return

host = None
try:
host = pynag.Model.Host.objects.get_by_shortname(hostname)
if host:
print "==> Filtered by host_name"
except:
pass

if not host:
hosts = pynag.Model.Host.objects.filter(_bbb_salt=salt)
if hosts:
for host in hosts:
print "==> Host %s has the same server salt, deleting it" % (host['host_name'])
host.delete()

host = None

if not host:
host = pynag.Model.Host()
host.attribute_appendfield('hostgroups', 'mconf-live-servers')
host.attribute_appendfield('hostgroups', 'temporary-servers')
print "==> Created a new host"

host['use'] = 'generic-passive-host'
host['host_name'] = hostname
host['address'] = address
host['_bbb_salt'] = salt
host['_bbb_url'] = url

print host
host.set_filename(hosts_cfg)
host.save()
print "==> Added!"

def remove_host(hostname):
try:
host = pynag.Model.Host.objects.get_by_shortname(hostname)
print host
host.delete()
print "==> Removed!"
except KeyError:
print "==> Can't find the host %s" % (hostname)

def list_hosts(hostgroup=None):
if not hostgroup:
hosts = pynag.Model.Host.objects.all
elif not hostgroup in hostgroups:
print "==> %s is not a valid hostgroup" % (hostgroup)
return
else:
hosts = [host['host_name'] for host in pynag.Model.Host.objects.filter(hostgroups__contains=hostgroup)]
hosts.sort()
print hosts

def detail_host(hostname=None):
if not hostname:
for host in pynag.Model.Host.objects.all:
print host
return

try:
host = pynag.Model.Host.objects.get_by_shortname(hostname)
print host
print "==> Details printed!"
except KeyError:
print "==> Can't find the host %s" % (hostname)

def add_to_network(hostname):
try:
host = pynag.Model.Host.objects.get_by_shortname(hostname)
host.attribute_replacefield('hostgroups', 'temporary-servers', 'mconf-network-members')
host.save()
print host
print "==> Host added to the network!"
except KeyError:
print "==> Can't find the host %s" % (hostname)

def remove_from_network(hostname):
try:
host = pynag.Model.Host.objects.get_by_shortname(hostname)
host.attribute_replacefield('hostgroups', 'mconf-network-members', 'temporary-servers')
host.save()
print host
print "==> Host added to the network!"
except KeyError:
print "==> Can't find the host %s" % (hostname)

def update_metadata(json_file):
json_data = open(json_file)
data = json.load(json_data)

url = data['metadata']['server_url'].lower()
# do not add the server_url again
del data['metadata']['server_url']
if not url.startswith('http://'):
url = 'http://' + url
address = urlparse(url).netloc
hostname = address.split(':')[0]

try:
host = pynag.Model.Host.objects.get_by_shortname(hostname)
for attr in data['metadata'].iterkeys():
host['_' + attr] = data['metadata'][attr]
print host
host.save()
print "==> Updated host metadata!"
except KeyError:
print "==> Can't find the host %s" % (hostname)

json_data.close()
return

def clean():
for host in pynag.Model.Host.objects.filter(_bbb_salt__exists=True):
url = host['_bbb_url']
salt = host['_bbb_salt']
try:
host.delete()
except:
pass
add_host(url, salt)

if __name__ == '__main__':
pynag.Model.cfg_file = nagios_cfg

if len(sys.argv) == 1:
print_usage()
if sys.argv[1] == "reload":
reload(False)
elif sys.argv[1] == "add":
add_host(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "remove":
remove_host(sys.argv[2])
elif sys.argv[1] == "detail":
detail_host(sys.argv[2] if len(sys.argv) > 2 else None)
elif sys.argv[1] == "list":
list_hosts(sys.argv[2] if len(sys.argv) > 2 else None)
elif sys.argv[1] == "add-to-network":
add_to_network(sys.argv[2])
elif sys.argv[1] == "remove-from-network":
remove_from_network(sys.argv[2])
elif sys.argv[1] == "update-metadata":
update_metadata(sys.argv[2])
elif sys.argv[1] == "clean":
clean()
elif sys.argv[1] == "force-reload":
reload(True)
else:
print_usage();

12 changes: 12 additions & 0 deletions libexec/mconf-hosts/metadata/prav1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"metadata": {
"server_alias":"PRAV-UFRGS 1",
"server_url":"http://mconf.org:8888",
"server_location":"Porto Alegre-RS, Brazil",
"institution_name":"GT-Mconf",
"institution_website":"http://mconf.org",
"institution_portal":"http://mconf.org",
"responsible_name":"Felipe Cecagno",
"responsible_email":"[email protected]"
}
}
3 changes: 3 additions & 0 deletions libexec/mconf-hosts/reset-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

sed -i "s:`pwd`/../../etc:/usr/local/nagios/etc:g" ../../etc/nagios.cfg
3 changes: 3 additions & 0 deletions libexec/mconf-hosts/setup-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

sed -i "s:/usr/local/nagios/etc:`pwd`/../../etc:g" ../../etc/nagios.cfg

0 comments on commit ab8565d

Please sign in to comment.