summaryrefslogtreecommitdiffstats
path: root/abs/core/supplemental-web/contents/fe_status.py
blob: c66d8f97f7772e2f43432045d0bbcb0fbc804571 (plain)
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
#!/usr/bin/python2

import urllib2
import xml.etree.ElementTree as ET
import socket
from  MythTV import Frontend
import sys
#socket.setdefaulttimeout(1)
#socket.setdefaulttimeout(.00001)

def msg(msg):
    #if cmdargs.silent is False:
    print "%s" %msg


def parse_xml(frontend):
    temp_dict = {'state':" --- ",
                 'title':" --- ",
                 'subtitle':" --- ",
                 'location':" --- "}
    url = "http://%s:6547/Frontend/GetStatus" %frontend
    try:
        request = urllib2.Request(url)
        xml = urllib2.urlopen(request,timeout=1)
        tree = ET.parse(xml)
        root = tree.getroot()
    except:
        #msg("   Couldn't connect to %s" %frontend)
        temp_dict['state'] = "offline"
        return temp_dict

    lst = root.find("State")
    for item in lst:
            try:
                keyitem = (item.find('Key').text).strip()
                valueitem  = (item.find('Value').text).strip()
            except:
                continue
            if keyitem == "state":
                temp_dict['state'] = valueitem

            elif keyitem == "title":
                temp_dict['title'] = valueitem

            elif keyitem == "subtitle":
                temp_dict['subtitle'] = valueitem

            elif keyitem == "currentlocation":
                temp_dict['location'] = valueitem

    return temp_dict


def mythfe_status(cursor,mythDB):
    status_dict={}
    try:
        #frontends = mythDB.getFrontends() #use cursor instead so it doesn't test connection
        cursor.execute("select distinct hostname from settings where hostname is not null;")
        frontends=cursor.fetchall()
    except:
        #msg("Exceptions")
        return

    for i in frontends:
        i=''.join(i)
        try:
            fe_hostname = socket.gethostbyaddr(i)[0]
        except:
            #print "DNS lookup failed for %s" %i
            fe_hostname = i
        try:
            ip_addr = socket.gethostbyname(i)
            fe_hostname = "%s (%s)" %(fe_hostname,ip_addr)
            status_dict[fe_hostname] = parse_xml(ip_addr)
        except:
            fe_hostname = "%s" %fe_hostname
            status_dict[fe_hostname] = parse_xml(fe_hostname)
    return status_dict


def print_html(status_dict):
    print '<div> <p></p>'
    print '</br> </br> '
    print '<table class="calllog">'

    #print "Current state of all online frontends"
    #print '</br> </br> '

    #print status_dict
    row = '''
<tr>
    <td> %s  </td>
    <td> %s  </td>
    <td> %s  </td>
    <td> %s  </td>
    <td> %s  </td>
</tr>
'''
    print row %("  MythFrontend Host  ","  State  ","  Title  ","  Subtitle  ","  MythFrontend Location  ")
    for fe in status_dict.keys():
        temp_dict = status_dict[fe]
        print row %(fe,
                    temp_dict['state'],
                    temp_dict['title'],
                    temp_dict['subtitle'],
                    temp_dict['location'])

    print "</table>"
    print '</div>'

#    print '''
#    <div id="footer">
#       </br>
#       <p>Offline systems are not listed</p>
#    </div>
#'''


def main():
    try:
        from MythTV import MythDB
        mythDB = MythDB()
        cursor = mythDB.cursor()
        db_conn=True
    except:
        msg("Couldn't connect to MythTV database.")
        db_conn=False

    try:
        from MythTV import MythBE
        mythBE = MythBE()
        be_conn=True
    except:
        msg("Couldn't connect to MythTV backend.")
        be_conn=False

    if (db_conn):
        status_dict = mythfe_status(cursor,mythDB)
        print_html(status_dict)



if __name__ == "__main__":
    main()