From 03e0b6fc6c74da067ea9d2b5a08721c914fb8cb1 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Mon, 1 Oct 2012 10:56:10 -0500
Subject: xymon:  added grouping of hosts by system type to the display.

---
 abs/core/xymon/PKGBUILD        |  12 +--
 abs/core/xymon/hobbitadd.py    | 180 ++++++++++++++++++++++++++---------------
 abs/core/xymon/hosts.cfg.patch |  10 +++
 abs/core/xymon/xymon.install   |   2 +-
 4 files changed, 134 insertions(+), 70 deletions(-)
 create mode 100644 abs/core/xymon/hosts.cfg.patch

diff --git a/abs/core/xymon/PKGBUILD b/abs/core/xymon/PKGBUILD
index 6b05078..a03e135 100755
--- a/abs/core/xymon/PKGBUILD
+++ b/abs/core/xymon/PKGBUILD
@@ -1,14 +1,14 @@
 pkgbase=xymon
 pkgname=(xymonserver xymonclient)
 pkgver=4.3.5
-pkgrel=36
+pkgrel=39
 pkgdesc="Hobbit is a system for monitoring servers and networks. "
 license="GPL"
 arch=('i686' 'x86_64')
 
 url="http://www.xymon.com/"
 
-source=(http://voxel.dl.sourceforge.net/project/xymon/Xymon/4.3.5/xymon-4.3.5.tar.gz  hobbitadd.py xymon_ghost_check graph.cfg.diff xymonserver.cfg.diff tasks.d.mv clientlaunch.cfg.patch hobbitstorage.py  hbnotes.py hobbit-myth-orphan.sh  analysis.cfg hbfunc.py be.png tuner.png tv.png  hobbit-mem-myth.sh hobbit_notify.sh alerts.cfg hobbit_myth_data.py logrotate-server.xymon logrotate-client.xymon log_list.sh client-local.cfg.patch led_themes.tar.gz xymon-server.rules)
+source=(http://voxel.dl.sourceforge.net/project/xymon/Xymon/4.3.5/xymon-4.3.5.tar.gz  hobbitadd.py xymon_ghost_check graph.cfg.diff xymonserver.cfg.diff tasks.d.mv clientlaunch.cfg.patch hobbitstorage.py  hbnotes.py hobbit-myth-orphan.sh  analysis.cfg hbfunc.py be.png tuner.png tv.png  hobbit-mem-myth.sh hobbit_notify.sh alerts.cfg hobbit_myth_data.py logrotate-server.xymon logrotate-client.xymon log_list.sh client-local.cfg.patch led_themes.tar.gz xymon-server.rules hosts.cfg.patch)
 
 depends=(rrdtool fping pcre python2 graphviz 'linhes-scripts>=7-24')
 
@@ -84,7 +84,8 @@ package_xymonserver(){
       patch -p0 <  $startdir/src/graph.cfg.diff || return 1
       patch -p0 <  $startdir/src/xymonserver.cfg.diff || return 1
       patch -p3 < $startdir/src/client-local.cfg.patch || return 1
-
+      patch -p3 < $startdir/src/hosts.cfg.patch || return 1
+      
       #fix permissions
       chmod 775 $startdir/pkg/xymonserver/data/srv/httpd/htdocs/
       chmod 775 $startdir/pkg/xymonserver/data/srv/httpd/htdocs/xymon/
@@ -112,7 +113,7 @@ package_xymonclient(){
     }
 
 md5sums=('31923ec126fe1c264fceb459d2175161'
-         '837f1073b5674c88534f93b03de27b6f'
+         'a0fbb9cbeb3600a40943e6205790f22f'
          'c60b0d59bb39505ee3d62c6dd65429c5'
          'a45dd4cdb84817bcf3e51b25cc713799'
          '034658a1084510ecbbb0dc166c0401eb'
@@ -135,4 +136,5 @@ md5sums=('31923ec126fe1c264fceb459d2175161'
          '98e9242ae346f729b14cb195786571f2'
          '31ac5c1f6dcc9408b64c4107b325a9a6'
          'c7178bbf384bbe86c318b60fef6faf10'
-         '72bf7caf0f53928d97ff5cd9810d6d4d')
+         '72bf7caf0f53928d97ff5cd9810d6d4d'
+         '606f535a49f32b35802ed9d46bd7d068')
diff --git a/abs/core/xymon/hobbitadd.py b/abs/core/xymon/hobbitadd.py
index c3c47ba..4ce32a2 100644
--- a/abs/core/xymon/hobbitadd.py
+++ b/abs/core/xymon/hobbitadd.py
@@ -6,26 +6,12 @@
 import urllib2
 import string
 import sys
-
-
-def readbb():
-    global bblist
-    try:
-        infile = open('/home/xymon/etc/hosts.cfg', 'r')
-    except(IOError), e:
-        print "couldn't open bb-hosts file"
-        sys.exit(1)
-    else:
-        bblist = infile.readlines()
-
-    infile.close()
+import MySQLdb
+import re
 
 
 def findghosts():
-    global infile
-    global bblist
-    global ghostitems
-    global numberitems
+    ghostitems=[]
     try:
         f = urllib2.urlopen("http://localhost/xymon/hobbit-cgi/ghostlist.sh?SORT=name&MAXAGE=300&TEXT")
     except urllib2.HTTPError, e:
@@ -35,61 +21,127 @@ def findghosts():
     else:
         ghostitems_full = f.readlines()
         f.close()
-        ghostitems=[]
+        #'''<h3><center>Failed to retrieve ghostlist from server</center></h3> #'''
         for i in ghostitems_full:
-            if not i.startswith('127.0.0'):
-               ghostitems.append(i)
+            if i.find("Failed to retrieve") >= 0  :
+                print "exiting because xymon ghost list failed"
+                print i
+                sys.exit(1)
+            if i.find('127.0.0') == -1 :
+               ghostitems.append(i.strip())
+               print "ghost host found: %s" %i.strip()
             else:
                continue
-        numberitems = len(ghostitems)
-
-
-def makenewbb():
-    global bblist
-    global ghostitems
-    global numberitems
-    global numinserts
-    numinserts=0
-    outlist = []
-    for item in bblist:
-        if item not in outlist:
-            outlist.append(item)
-
-    for item in ghostitems:
-        line = item.rstrip()
-        nline = "%s #func" % (line)
-        nline = nline + '\n'
-        if nline not in outlist:
-            outlist.append(nline)
-            print "adding: " + nline
-            numinserts = numinserts + 1
-    if numinserts > 0:
-        outfile = open("/home/xymon/etc/hosts.cfg","w")
-        #outfile = open("/tmp/new-bb","w")
-        for i in outlist:
-            outfile.write(i + '\n' )
-        outfile.close
+    #ghostitems = ['192.168.1.233\t\tfeste32', '192.168.1.127\t\tjams3']
+    return ghostitems
 
+def readbb():
+    bblist = []
+    strip_list = []
+    try:
+        infile = open('/home/xymon/etc/hosts.cfg', 'r')
+        #infile = open('/tmp/old-bb', 'r')
+    except(IOError), e:
+        print "couldn't open hosts.cfg file"
+        sys.exit(1)
+    else:
+        bblist = infile.readlines()
+        for line in bblist:
+            strip_list.append(line.strip())
+
+
+    infile.close()
+    return strip_list
+
+
+def ghost_type(ghostlist):
+    match_gdict={}
+    #query db with ghostname
+    db = MySQLdb.connect(host="localhost", user="mythtv", passwd="mythtv", db="mythconverg")
+    # create a cursor
+    cursor = db.cursor()
+    for item in ghostlist:
+        host = item.split()[1].strip()
+        cursor.execute("select value,data  from settings where hostname=%s and value in ('HostSystemType');",(host))
+        hostvalues=cursor.fetchall()
+        #this is here in case the db fails
+        if len(hostvalues) == 0:
+            hostvalues = (('HostSystemType','unknown system'),)
+
+        #should return something like this(('HostSystemType', 'Frontend_only'),)
+        #hostvalues = (('HostSystemType', 'Frontend_only'),)
+        if  len(hostvalues) == 1:
+            #get current systemtype and make it all lowercase
+            current_hostvalue = hostvalues[0][1].lower()
+            try:
+                value = match_gdict[current_hostvalue]
+                value.append(item)
+                match_gdict[current_hostvalue] = value
+            except:
+                match_gdict[current_hostvalue] = [item]
+
+
+    #{'Frontend_only': ['feste32', 'feste3', 'feste33']}
+    return match_gdict
+
+def check_duplicate(host_list,server):
+    server_no_space=re.sub(r'\s', '',server)
+    for line in host_list:
+        line_no_space=re.sub(r'\s', '',line)
+
+        if line_no_space.find(server_no_space) > -1  :
+            #print "server in hostlist"
+            return True
+        else:
+            continue
+
+
+    return False
+
+def create_new_hosts(matched_gdict,host_list):
+    #loop over key in gdict
+    for key,value in matched_gdict.iteritems():
+        groupline = "title %s" %key
+        #print key
+        if key == "unknown system":
+            host_append_line = "%s #"
+        else:
+            host_append_line = "%s #func"
+
+        if groupline in host_list:
+            insert_index = host_list.index(groupline) + 1
+            for server in value:
+                if check_duplicate(host_list,server) == False :
+                    #host_append_line = "%s #func" %(server)
+                    host_list.insert(insert_index,host_append_line %server)
+        else:
+            host_list.append(groupline)
+            for server in value:
+                #host_append_line = "%s #func" %(server)
+                host_list.append(host_append_line %server)
+
+
+    outfile = open("/home/xymon/etc/hosts.cfg","w")
+    #outfile = open("/tmp/new-bb","w")
+    for i in host_list:
+        outfile.write(i + '\n' )
+        outfile.close
+    return
 
-global infile
-global bblist
-global ghostitems
-global numberitems
-global numinserts
-numinserts=0
-readbb()
-findghosts()
 
 
-if numberitems > 0:
-    makenewbb()
-#else:
-#    print "No hosts to add"
+def go():
+    ghostlist = findghosts()
+    #print ghostlist
 
+    if len(ghostlist) == 0 :
+        sys.exit()
 
-if numinserts < 1:
-    sys.exit(1)
-else:
-    sys.exit(0)
+    #match ghosts to system type
+    matched_gdict = ghost_type(ghostlist)
 
+    host_list = readbb()
+    create_new_hosts(matched_gdict,host_list)
 
+if __name__ == "__main__":
+    go()
diff --git a/abs/core/xymon/hosts.cfg.patch b/abs/core/xymon/hosts.cfg.patch
new file mode 100644
index 0000000..6968a07
--- /dev/null
+++ b/abs/core/xymon/hosts.cfg.patch
@@ -0,0 +1,10 @@
+--- home/xymon/etc/hosts.cfg.orig	2012-09-30 23:04:38.229515544 +0000
++++ home/xymon/etc/hosts.cfg	2012-09-30 23:04:50.676181831 +0000
+@@ -11,6 +11,6 @@
+ #    man-page.
+ #
+ # You need to define at least the Xymon server itself here.
+-
++title primary server
+ 127.0.0.1   localhost      # bbd http://localhost/
+ 
diff --git a/abs/core/xymon/xymon.install b/abs/core/xymon/xymon.install
index 3abec38..36bbbaa 100644
--- a/abs/core/xymon/xymon.install
+++ b/abs/core/xymon/xymon.install
@@ -15,7 +15,7 @@ post_install() {
 
 
 	sed -i -e "s/localhost/$hostname/" /home/xymon/server/etc/hosts.cfg
-	sed -i -e "s/\#/\# func/" /home/xymon/server/etc/hosts.cfg
+	#sed -i -e "s/\#/\# func/" /home/xymon/server/etc/hosts.cfg
 	sed -i -e "s/localhost/$hostname/" /home/xymon/server/etc/xymonserver.cfg
 	chown -R nobody /home/xymon
 	chown -R nobody /data/srv/httpd/htdocs/xymon
-- 
cgit v0.12

