From 82e79deb33a3e21737fa28d15cea63b4ae151037 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Wed, 27 Nov 2013 16:37:56 -0600
Subject: LinHES-system:  myth_status.py  Add in the ability to pick up system
 alerts which are generated by xymon. The program will look for files placed
 in /home/xymon/var/login_notify and parse as needed. Files are generated by
 xymon by the file login_notify.py  which is called via alerts.cfg

Also myth_status got a bit of rearranging.  Now the tuner & recording status info is in a class.

refs #943
refs #945
---
 abs/core/LinHES-system/myth_status.py | 302 +++++++++++++++++++++++++---------
 1 file changed, 220 insertions(+), 82 deletions(-)

diff --git a/abs/core/LinHES-system/myth_status.py b/abs/core/LinHES-system/myth_status.py
index f0e41f3..0f2edfb 100644
--- a/abs/core/LinHES-system/myth_status.py
+++ b/abs/core/LinHES-system/myth_status.py
@@ -1,21 +1,12 @@
 #!/usr/bin/python2
+#This program is called on login to display the status of mythtv tuners & recording status
+#Also will display alerts generated by xymon.  If the location of xymon changes, this script needs to be updated.
+
 from MythTV import MythBE,MythDB,MythLog
 import datetime,time,sys,subprocess,re
-try:
-    be = MythBE()
-    db = MythDB()
-except:
-    print "\nCouldn't connect to MythTV service for status"
-    sys.exit(1)
-
-cursor = db.cursor()
-now = datetime.datetime.now()
-farout=99999999
-next_start_diff=datetime.timedelta(farout)
-num_upcoming=12
+import os,glob
 
 def formatTD(td):
-    #print td
     days = td.days
     hours = td.seconds // 3600
     minutes = (td.seconds % 3600) // 60
@@ -46,76 +37,223 @@ def formatTD(td):
     return_string = '%s%s%s%s' % (day_string, hour_string, minute_string, second_string)
     return return_string
 
+def print_alerts():
+    dir_name = "/home/xymon/var/login_alerts"
+    out_alert=""
+    try:
+        os.chdir(dir_name)
+    except:
+        pass
+        #print "    myth_status: Couldn't change dir to %s" %dir_name
 
+    file_list=glob.glob("*")
+    
+    if len(file_list) == 0:
+        #print "    myth_status:  no alert files found"
+        pass
+    else:
+        for alert_file in file_list:
+            out_line=''
+            datahost = ''
+            dataservice = ''
+            datadown = ''
+            try:
+                #print "    myth_staus: reading in %s" %alert_file
+                f=open(alert_file,'r')
+                lines=f.readlines()
+                f.close()
+            except:
+                #print "    myth_status: Couldn't open %s for reading" %alert_file
+                continue
+            
+            for line in lines:
+                try:
+                    data,value=line.split(":")
+                except:
+                    continue  #exception occured try the next line
+                
+                if data == 'HOST':
+                    datahost = value.strip()
+                elif data == 'SERVICE':
+                    dataservice = value.strip()
+                elif data == 'DOWN':
+                    datadown = value.strip()
+                    sec=int(datadown)
+                    td_sec = datetime.timedelta(seconds=sec)
+                    td_sec_formated = formatTD(td_sec)
+            
+            out_line ="    %s on %s  down for %s \n" %(dataservice,
+                                                           datahost,
+                                                           td_sec_formated)
+            out_alert += out_line
+                
+        print "System Alerts:"
+        print "---------------"
+        if len(out_alert) > 0:
+            print out_alert 
+            print "    Please see the system health webpage for more information"
+        else:
+            print "    All systems OK"
 
-a=be.getRecorderList()
-header="#"*60
-print header
-print ""
-print "Tuner Status:"
-print "-------------"
-for i in a:
-    cmd="select cardtype,hostname  from capturecard where cardid=%s;" %i
+    return 
 
-    cursor.execute(cmd)
-    results=cursor.fetchall()
-    type = results[0][0]
-    hostname = results[0][1]
-    id = i
-    try:
-        c=be.getCurrentRecording(i)
-        if c.title ==  None:
-            current_recording = "Idle"
+
+#-------------------------------------------
+
+
+class tuner_recording_status:
+    def __init__ (self,num_upcoming):
+        
+        self.now = datetime.datetime.now()
+        self.farout=99999999
+        self.next_start_diff=datetime.timedelta(self.farout)
+        self.num_upcoming=num_upcoming
+        self.tuner_status_list=[]
+        self.conflict_list=[]
+        self.upcoming_list=[]
+        self.ur=0
+        self.db_connection_status = self.check_database_connection()
+        if self.db_connection_status == 0:
+            self.tuner_status()
+            self.conflicts()
+            self.upcoming_recordings()
+
+    def get_db_check_status(self):
+        return self.db_connection_status
+        
+    def check_database_connection(self):
+        rc=0
+        try:
+            self.be = MythBE()
+            self.db = MythDB()
+            self.cursor = self.db.cursor()
+        except:
+            print "\nCouldn't connect to MythTV service for status"
+            rc=1
+        return rc
+#-----
+    def tuner_status(self):
+        a=self.be.getRecorderList()
+        for i in a:
+            outline=''
+            cmd="select cardtype,hostname  from capturecard where cardid=%s;" %i
+            self.cursor.execute(cmd)
+            results=self.cursor.fetchall()
+            type = results[0][0]
+            hostname = results[0][1]
+            id = i
+            try:
+                c=self.be.getCurrentRecording(i)
+                if c.title ==  None:
+                    current_recording = "Idle"
+                else:
+                    current_recording = "Recording %s" %c.title
+                outline = "    Tuner %s (%s) on %s : %s " %(id, type, hostname, current_recording)
+                self.tuner_status_list.append(outline)
+            except:
+                outline = "    Tuner %s (%s) on %s : %s " %(id, type, hostname, "Tuner Error")
+                self.tuner_status_list.append(outline)
+
+    def get_tuner_status(self):
+        return self.tuner_status
+    
+    def print_tuner_status(self):
+        print "Tuner Status:"
+        print "-------------"
+        if len(self.tuner_status_list) > 0 :
+            for line in self.tuner_status_list:
+                print line
         else:
-            current_recording = "Recording %s" %c.title
-        print "    Tuner %s (%s) on %s : %s " %(id, type, hostname, current_recording)
-    except:
-        print "    Tuner %s (%s) on %s : %s " %(id, type, hostname, "Tuner Error")
-
-print ""
-print "Upcoming Recordings (Next %s Scheduled):" %(num_upcoming)
-print "----------------------------------------"
-a=be.getUpcomingRecordings()
-r=0
-for i in a:
-    r += 1
-    if r > num_upcoming:
-        break
-
-    title_chan="%s (%s)" %(i.title, i.channame)
-    #remove timezone
-    start_time=re.split("[-+]\d\d:\d\d",str(i.starttime))[0]
-    start_time_struct=datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
-    start_time_out=start_time_struct.strftime("%a %b %d %I:%M%p")
-    print "    %s - %s - %s" %(start_time_out,i.hostname, title_chan)
-    diff = start_time_struct - now
-    if diff < next_start_diff :
-        next_start_diff = diff
-
-
-print ""
-print "Recording Conflicts:"
-print "----------------------"
-a=be.getConflictedRecordings()
-c=0
-for i in a:
-    title_chan="%s (%s)" %(i.title, i.channame)
-    print "    %s -  %-50s " %(i.starttime,title_chan)
-    c=c+1
-
-if c == 0:
-    print "    No Conflicts"
-
-print ""
-
-if next_start_diff == datetime.timedelta(farout):
-   ur="No recordings are scheduled"
-else:
-    ur=formatTD(next_start_diff)
-print "The next recording starts in:"
-print "-----------------------------"
-print "    %s" %(ur)
-print ""
-subprocess.call("/usr/LH/bin/diskspace.sh",shell=True)
-print ""
-print header
+            print "    No tuners found"
+            
+#--------
+    def upcoming_recordings(self):
+        
+        a=self.be.getUpcomingRecordings()
+        r=0
+        for i in a:
+            r += 1
+            if r > self.num_upcoming:
+                break
+            
+            title_chan="%s (%s)" %(i.title, i.channame)
+            #remove timezone
+            start_time=re.split("[-+]\d\d:\d\d",str(i.starttime))[0]
+            start_time_struct=datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
+            start_time_out=start_time_struct.strftime("%a %b %d %I:%M%p")
+            self.upcoming_list.append([start_time_out,i.hostname, title_chan])
+            #print "    %s - %s - %s" %(start_time_out,i.hostname, title_chan)
+            diff = start_time_struct - self.now
+            if diff < self.next_start_diff :
+                self.next_start_diff = diff
+
+        if self.next_start_diff == datetime.timedelta(self.farout):
+            self.ur="No recordings are scheduled"
+        else:
+            self.ur=formatTD(self.next_start_diff)
+            
+
+    def get_upcoming_recordings(self):
+        return self.upcoming_list
+    
+    def print_upcoming_recordings(self):
+        #print self.get_upcoming_recordings()
+        print ""
+        print "Upcoming Recordings (Next %s Scheduled):" %(self.num_upcoming)
+        print "----------------------------------------"
+        if len(self.get_upcoming_recordings()) > 0:
+            for i in self.get_upcoming_recordings():
+                #print "    %s - %s - %s" %(start_time_out,i.hostname, title_chan)
+                print "    %s - %s - %s" %(i[0],i[1],i[2])
+        else:
+            print "    No upcoming recordings"
+        pass
+
+    def get_next_start_time(self):
+        return self.ur
+    
+    def print_next_start_time(self):
+        print ""
+        print "The next recording starts in:"
+        print "-----------------------------"
+        print "    %s" %(self.get_next_start_time())
+        print ""
+    
+#-----
+
+    def conflicts(self):
+        a=self.be.getConflictedRecordings()
+        for i in a:
+            out_line=''
+            title_chan="%s (%s)" %(i.title, i.channame)
+            out_line=(i.starttime,title_chan)
+            self.conflict_list.append(out_line)
+
+    def get_conflict_list(self):
+        return self.conflict_list
+    
+    def print_conflict_list(self):
+        print ""
+        print "Recording Conflicts:"
+        print "----------------------"
+        if len(self.get_conflict_list()) > 0:
+            for i in self.get_conflict_list():
+                print "    %s -  %-50s " %(i[0],i[1])
+        else:
+            print "    No conflicts"
+
+#header="#"*60
+
+
+def go():
+    tuner = tuner_recording_status(12)
+    if tuner.get_db_check_status() == 0:
+        tuner.print_tuner_status()
+        tuner.print_upcoming_recordings()
+        tuner.print_conflict_list()
+        tuner.print_next_start_time()
+    print_alerts()
+
+
+if __name__ == "__main__":
+    go()
-- 
cgit v0.12

