-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSP-W215-exporter.py
executable file
·100 lines (74 loc) · 2.51 KB
/
DSP-W215-exporter.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
#!/usr/bin/env python
from prometheus_client import start_http_server, Gauge, Enum, Info
from pyW215.pyW215 import SmartPlug, ON, OFF
import time
import argparse
version = 0.10
# Parse commandline arguments
parser = argparse.ArgumentParser(
description="D-Link DSP-W125 Wi-Fi Smart Plug Prometheus exporter v" +
str(version))
parser.add_argument(
"-t", "--target", metavar="<ip>", required=True,
help="Target IP address", type=str)
parser.add_argument(
"-f", "--frequency", metavar="<seconds>", required=False,
help="Interval in seconds between checking measures", default=1, type=int)
parser.add_argument(
"-p", "--port", metavar="<port>", required=False,
help="Port for exporter to listen on", default=8210,
type=int)
parser.add_argument(
"-c", "--code", metavar="<code>", required=True,
help="Secret code to access device api", type=str)
args = parser.parse_args()
# Set target IP, port and command to send
ip = args.target
listen_port = args.port
sleep_time = args.frequency
code = args.code
sp = None
# Send command and receive reply
# Create a metric to track time spent and requests made.
# Gaugage: it goes up and down, snapshot of state
REQUEST_POWER = Gauge('w125_power_watt', 'DSP-W125 Watt measure')
REQUEST_TEMP = Gauge('w125_temperature', 'DSP-W125 Temperature measure')
REQUEST_TOTAL = Gauge('w125_total', 'DSP-W125 Total energy measure')
REQUEST_STATE = Enum('w125_state', 'DSP-W125 switch status',
states=['ON', 'OFF', 'unknown'])
REQUEST_POWER.set_function(lambda: get_power())
REQUEST_TEMP.set_function(lambda: get_temp())
REQUEST_TOTAL.set_function(lambda: get_total())
def get_state():
""" Get W125 switch state """
return sp.state
def get_power():
""" Get W125 power """
val = sp.current_consumption
if val == 'N/A':
quit("Could not connect to host " + ip)
return 0
return val
def get_temp():
""" Get W125 temperature """
val = sp.temperature
if val == 'N/A':
quit("Could not connect to host " + ip)
return 0
return val
def get_total():
""" Get W125 total energy usage """
val = sp.total_consumption
if val == 'N/A':
quit("Could not connect to host " + ip)
return 0
return val
# Main entry point
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(listen_port)
# Main loop
while True:
sp = SmartPlug(ip, code)
REQUEST_STATE.state(state=get_state())
time.sleep(sleep_time)