forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics.py
executable file
·174 lines (134 loc) · 4.01 KB
/
diagnostics.py
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
#!/usr/bin/env python
import netifaces
import os
import sh
import socket
import sqlite3
import utils
from pprint import pprint
from uptime import uptime
from datetime import datetime
def parse_cpu_info():
cpu_info = {
'cpu_count': 0
}
with open('/proc/cpuinfo', 'r') as f:
for line in f:
try:
key = line.split(':')[0].strip()
value = line.split(':')[1].strip()
except:
pass
if key == 'processor':
cpu_info['cpu_count'] += 1
if key in ['Serial', 'Hardware', 'Revision', 'model name']:
cpu_info[key.lower()] = value
return cpu_info
def get_kernel_modules():
modules = []
try:
for line in sh.lsmod():
if 'Module' not in line:
modules.append(line.split()[0])
return modules
except:
return 'Unable to run lsmod.'
def get_gpu_version():
try:
version = sh.vcgencmd('version')
for line in version:
if 'version' in line:
return line.strip().replace('version ', '')
except:
return 'Unable to run vcgencmd.'
def get_monitor_status():
try:
return sh.tvservice('-s').stdout.strip()
except:
return 'Unable to run tvservice.'
def get_network_interfaces():
if_data = {}
for interface in netifaces.interfaces():
if_data[interface] = netifaces.ifaddresses(interface)
return if_data
def get_uptime():
return uptime()
def get_playlist():
screenly_db = os.path.join(os.getenv('HOME'), '.screenly/screenly.db')
playlist = []
if os.path.isfile(screenly_db):
conn = sqlite3.connect(screenly_db)
c = conn.cursor()
for row in c.execute('SELECT * FROM assets;'):
playlist.append(row)
c.close
return playlist
def get_load_avg():
"""
Returns load average rounded to two digits.
"""
load_avg = {}
get_load_avg = os.getloadavg()
load_avg['1 min'] = round(get_load_avg[0], 2)
load_avg['5 min'] = round(get_load_avg[1], 2)
load_avg['15 min'] = round(get_load_avg[2], 2)
return load_avg
def get_git_hash():
screenly_path = os.path.join(os.getenv('HOME'), 'screenly', '.git')
try:
get_hash = sh.git(
'--git-dir={}'.format(screenly_path),
'rev-parse',
'HEAD'
)
return get_hash.stdout.strip()
except:
return 'Unable to get git hash.'
def try_connectivity():
urls = [
'http://www.google.com',
'http://www.bbc.co.uk',
'https://www.google.com',
'https://www.bbc.co.uk',
]
result = []
for url in urls:
if utils.url_fails(url):
result.append('{}: Error'.format(url))
else:
result.append('{}: OK'.format(url))
return result
def ntp_status():
query_ntp = sh.ntpq('-p')
return query_ntp.stdout
def get_utc_isodate():
return datetime.isoformat(datetime.utcnow())
def get_debian_version():
debian_version = '/etc/debian_version'
if os.path.isfile(debian_version):
with open(debian_version, 'r') as f:
for line in f:
return str(line).strip()
else:
return 'Unable to get Debian version.'
def compile_report():
report = {}
report['cpu_info'] = parse_cpu_info()
report['uptime'] = get_uptime()
report['kernel_modules'] = get_kernel_modules()
report['monitor'] = get_monitor_status()
report['ifconfig'] = get_network_interfaces()
report['hostname'] = socket.gethostname()
report['playlist'] = get_playlist()
report['git_hash'] = get_git_hash()
report['connectivity'] = try_connectivity()
report['loadavg'] = get_load_avg()
report['ntp_status'] = ntp_status()
report['utc_isodate'] = get_utc_isodate()
report['debian_version'] = get_debian_version()
report['gpu_version'] = get_gpu_version()
return report
def main():
pprint(compile_report())
if __name__ == "__main__":
main()