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
|
"""
Read, Parse, Post Stats to InfluxDB.
"""
import Common.sensors as sensors
import Common.influx as influx
import Common.net as net
import Common.fs as fs
import socket
import argparse
# #############################################################################
# Parse Arguments
# #############################################################################
parser = argparse.ArgumentParser()
parser.add_argument('--netatmo', action='store_true', \
help="Query and Push Netatmo Data.")
parser.add_argument('--vpn', action='store_true', \
help="Query and Push VPN Status.")
parser.add_argument('--fs', action='store_true', \
help="Filesystem Stats.")
args = parser.parse_args()
# Sanity Check
if not args.netatmo and not args.vpn and not args.fs:
raise Exception('Nothing Queried. Terminating.')
# #############################################################################
# Load Data from Netatmo
# #############################################################################
if args.netatmo:
readings, epochs = \
sensors.get_netatmo_readings(home='Lv114',\
modules=['Living Room', \
'Balcony/SW'])
# #############################################################################
# Check VPN Status
# #############################################################################
if args.vpn:
is_vpn_connected = net.is_vpn_connected()
# #############################################################################
# Get Filesystem Stats
# #############################################################################
if args.fs:
fs_usage = fs.get_fs_usage(fs='/volume1')
# #############################################################################
# Get Hostname
# #############################################################################
hostname = socket.gethostname()
# #############################################################################
# Build Data Post
# NB: For InfluxDB >=0.9.3, integer data points require a trailing i.
# For example, ncpus_allocated,parititon=cpu value=5i
# #############################################################################
lines = []
# Netatmo w/ Sanitized Strings (Remove Caps, Spaces to Underscore)
if args.netatmo:
for module_name, module_values in zip(readings.keys(), readings.values()):
for value_name, value in zip(module_values.keys(), module_values.values()):
line = "%s,module=%s value=%.2f %i" % \
( value_name.lower(), \
'_'.join(module_name.lower().split(' ')), \
value, \
epochs[module_name] )
lines.append(line)
# VPN Status
if args.vpn:
line = "is_vpn_connected,host=%s value=%ii" % \
( hostname, is_vpn_connected )
lines.append(line)
# Filesystem Status
if args.fs:
line_01 = "fs_free,host=%s value=%ii" % ( hostname, fs_usage['free'] )
line_02 = "fs_total,host=%s value=%ii" % ( hostname, fs_usage['total'] )
line_03 = "fs_used,host=%s value=%ii" % ( hostname, fs_usage['used'] )
lines.append(line_01)
lines.append(line_02)
lines.append(line_03)
# Join
data = "\n".join(lines)
# #############################################################################
# Post Data
# #############################################################################
influx.post_data(data)
|