1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#!/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
from dateutil.parser import parse
from dateutil import tz
import os,glob
from socket import gethostname;
def formatTD(td):
days = td.days
hours = td.seconds // 3600
minutes = (td.seconds % 3600) // 60
seconds = td.seconds % 60
if days == 0:
day_string = ""
elif days > 1:
day_string = "%s days, " %days
else:
day_string = "%s day, " %days
if hours > 1:
hour_string = "%s hours, " %hours
else:
hour_string = "%s hour, " %hours
if minutes > 1:
minute_string = "%s minutes, " %minutes
else:
minute_string = "%s minute, " %minutes
if seconds > 1:
second_string = "%s seconds" %seconds
else:
second_string = "%s second" %seconds
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 = ''
datacolor = ''
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 == 'COLOR':
datacolor = 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 %s for %s \n" %(dataservice,
datahost,datacolor.upper(),
td_sec_formated)
out_alert += out_line
print "System Alerts:"
print "--------------"
if len(out_alert) > 0:
print out_alert
print " Go to http://%s and click Health & Maintenance for more information." %gethostname()
else:
print " All systems OK"
return
#-------------------------------------------
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:
print " No tuners found"
#--------
def upcoming_recordings(self):
a=self.be.getUpcomingRecordings()
r=0
currTZ = tz.tzlocal()
for i in a:
r += 1
if r > self.num_upcoming:
break
title_chan="%s (%s)" %(i.title, i.channame)
# convert timezone to local timezone
start_time=parse(str(i.starttime))
start_time=start_time.astimezone(currTZ)
start_time_out=start_time.strftime("%a %b %d %I:%M%p")
self.upcoming_list.append([start_time_out,i.hostname, title_chan])
#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")
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()
currTZ = tz.tzlocal()
for i in a:
out_line=''
title_chan="%s (%s)" %(i.title, i.channame)
# convert timezone to local timezone
start_time=parse(str(i.starttime))
start_time=start_time.astimezone(currTZ)
start_time_out=start_time.strftime("%a %b %d %I:%M%p")
out_line=(start_time_out,i.hostname,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 - %s - %s" %(i[0],i[1],i[2])
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()
|