Skip to content

Commit a6381ef

Browse files
author
Konstantin Wolff
committed
done some code cleanup -> PEP8, fixed some logical stuff like not used variables in all sensors
1 parent e2f4265 commit a6381ef

File tree

18 files changed

+202
-192
lines changed

18 files changed

+202
-192
lines changed

miniprobe/miniprobe.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python
2-
#Copyright (c) 2014, Paessler AG <[email protected]>
3-
#All rights reserved.
4-
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
2+
# Copyright (c) 2014, Paessler AG <[email protected]>
3+
# All rights reserved.
4+
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
55
# following conditions are met:
6-
#1. Redistributions of source code must retain the above copyright notice, this list of conditions
6+
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions
77
# and the following disclaimer.
8-
#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
8+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
99
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
10-
#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
10+
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
1111
# or promote products derived from this software without specific prior written permission.
1212

13-
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
1414
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1515
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
1616
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
@@ -20,9 +20,9 @@
2020
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2121

2222

23-
### PRTG Python Miniprobe
24-
### Miniprobe needs at least Python 2.7 because of "importlib"
25-
### If older python version is used you will have to install "importlib"
23+
# PRTG Python Miniprobe
24+
# Miniprobe needs at least Python 2.7 because of "importlib"
25+
# If older python version is used you will have to install "importlib"
2626

2727
# import general modules
2828
import sys
@@ -149,4 +149,3 @@ def clean_mem():
149149
os.popen("sysctl vm.drop_caches=1")
150150
os.popen("sysctl vm.drop_caches=2")
151151
os.popen("sysctl vm.drop_caches=3")
152-

miniprobe/probe.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python
2-
#Copyright (c) 2014, Paessler AG <[email protected]>
3-
#All rights reserved.
4-
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
2+
# Copyright (c) 2014, Paessler AG <[email protected]>
3+
# All rights reserved.
4+
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
55
# following conditions are met:
6-
#1. Redistributions of source code must retain the above copyright notice, this list of conditions
6+
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions
77
# and the following disclaimer.
8-
#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
8+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
99
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
10-
#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
10+
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
1111
# or promote products derived from this software without specific prior written permission.
1212

13-
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
1414
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1515
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
1616
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
@@ -20,9 +20,9 @@
2020
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2121

2222

23-
### PRTG Python Miniprobe
24-
### Miniprobe needs at least Python 2.7 because of "importlib"
25-
### If older python version is used you will have to install "importlib"
23+
# PRTG Python Miniprobe
24+
# Miniprobe needs at least Python 2.7 because of "importlib"
25+
# If older python version is used you will have to install "importlib"
2626

2727
# import general modules
2828
import sys
@@ -46,7 +46,6 @@
4646
import multiprocessing
4747
except Exception as e:
4848
print e
49-
#sys.exit()
5049

5150
# Implemented for internal testing only. Not for public usage!
5251
http = False
@@ -94,8 +93,6 @@ def main():
9493
data_announce = mini_probe.create_parameters(config, announce_json, 'announce')
9594
logging.debug("Announce Data: %s" % data_announce)
9695
json_history = []
97-
timeout = False
98-
9996
while not announce:
10097
try:
10198
# announcing the probe and all sensors
@@ -107,8 +104,7 @@ def main():
107104
logging.info("ANNOUNCE request successfully sent to PRTG Core Server at %s:%s."
108105
% (config["server"], config["port"]))
109106
logging.debug("Connecting to %s:%s" % (config["server"], config["port"]))
110-
logging.debug("Status Code: %s | Message: %s" % (request_announce.status_code,
111-
request_announce.text))
107+
logging.debug("Status Code: %s | Message: %s" % (request_announce.status_code, request_announce.text))
112108
request_announce.close()
113109
except requests.exceptions.Timeout:
114110
logging.error("ANNOUNCE Timeout: " + str(data_announce))
@@ -137,8 +133,8 @@ def main():
137133
logging.debug(request_task.text)
138134
try:
139135
json_response = request_task.json()
140-
except Exception as e:
141-
logging.info("Error: %s! Server returned: %s" % (e, request_task.text))
136+
except Exception as ex:
137+
logging.info("Error: %s! Server returned: %s" % (ex, request_task.text))
142138
request_task.close()
143139
gc.collect()
144140
task = True
@@ -147,8 +143,8 @@ def main():
147143
logging.debug("task_url: " + url_task + "\ntask_data: " + str(task_data))
148144
except requests.exceptions.Timeout:
149145
logging.error("TASK Timeout: " + str(task_data))
150-
logging.debug("Timeout encountered. Need to write more code to handle timeoutzzzzz: %s" % json_history)
151-
timeout = True
146+
logging.debug("Timeout encountered. Need to write more code to handle timeoutzzzzz: %s"
147+
% json_history)
152148
except Exception as announce_error:
153149
logging.error(announce_error)
154150
time.sleep(int(config['baseinterval']) / 2)
@@ -178,13 +174,14 @@ def main():
178174
while len(json_payload_data) < len(element):
179175
out = out_queue.get()
180176
json_payload_data.append(out)
181-
except Exception as e:
182-
logging.error(e)
177+
except Exception as ex:
178+
logging.error(ex)
183179
pass
184180

185181
url_data = mini_probe.create_url(config, 'data', http)
186182
try:
187-
request_data = requests.post(url_data, data=json.dumps(json_payload_data), verify=False, timeout=30)
183+
request_data = requests.post(url_data, data=json.dumps(json_payload_data),
184+
verify=False, timeout=30)
188185
logging.info("DATA request successfully sent to PRTG Core Server at %s:%s. Status: %s"
189186
% (config["server"], config["port"], request_data.status_code))
190187
logging.debug("data_url: " + url_data + "\ndata_data: " + str(json_payload_data))

miniprobe/sensors/adns.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
pass
3333

3434

35-
class aDNS(object):
35+
class ADNS(object):
3636
def __init__(self):
3737
gc.enable()
3838

@@ -49,10 +49,12 @@ def get_sensordef():
4949
Definition of the sensor and data to be shown in the PRTG WebGUI
5050
"""
5151
sensordefinition = {
52-
"kind": aDNS.get_kind(),
52+
"kind": ADNS.get_kind(),
5353
"name": "DNS",
54-
"description": "Monitors a DNS server (Domain Name Service), resolves a domain name, and compares it to an IP address",
55-
"help": "The DNS sensor monitors a Domain Name Service (DNS) server. It resolves a domain name and compares it to a given IP address.",
54+
"description": "Monitors a DNS server (Domain Name Service), "
55+
"resolves a domain name, and compares it to an IP address",
56+
"help": "The DNS sensor monitors a Domain Name Service (DNS) server. "
57+
"It resolves a domain name and compares it to a given IP address.",
5658
"tag": "mpdnssensor",
5759
"groups": [
5860
{
@@ -114,18 +116,16 @@ def get_sensordef():
114116

115117
@staticmethod
116118
def get_data(data, out_queue):
117-
adns = aDNS()
118-
result = ""
119-
timed = 0
119+
adns = ADNS()
120120
logging.debug("Running sensor: %s" % adns.get_kind())
121121
try:
122122
start_time = timeit.default_timer()
123-
result = adns.get_record(data['timeout'],data['port'],data['domain'],data['type'],data['host'])
123+
result = adns.get_record(data['timeout'], data['port'], data['domain'], data['type'], data['host'])
124124
timed = timeit.default_timer() - start_time
125125
logging.debug("DNS: %s" % result)
126-
except Exception as e:
126+
except Exception as ex:
127127
logging.error("Ooops Something went wrong with '%s' sensor %s. Error: %s" % (adns.get_kind(),
128-
data['sensorid'], e))
128+
data['sensorid'], ex))
129129
data_r = {
130130
"sensorid": int(data['sensorid']),
131131
"error": "Exception",
@@ -134,7 +134,7 @@ def get_data(data, out_queue):
134134
}
135135
out_queue.put(data_r)
136136
return 1
137-
dns_channel = adns.get_dns(int(timed*1000))
137+
dns_channel = adns.get_dns(int(timed * 1000))
138138
addressdata = []
139139
for element in dns_channel:
140140
addressdata.append(element)
@@ -176,9 +176,9 @@ def get_record(timeout, port, domain, type, host):
176176
resolver.port = port
177177
if type == 'PTR':
178178
addr = dns.reversename.from_address(domain)
179-
answers = dns.resolver.query(addr,type)
179+
answers = dns.resolver.query(addr, type)
180180
else:
181-
answers = dns.resolver.query(domain,type)
181+
answers = dns.resolver.query(domain, type)
182182
if (type == 'A') or (type == 'AAAA'):
183183
for rdata in answers:
184184
result = result + str(rdata.address) + ", "
@@ -187,10 +187,12 @@ def get_record(timeout, port, domain, type, host):
187187
result = result + rdata.preference + ": " + rdata.exchange + ", "
188188
elif type == 'SOA':
189189
for rdata in answers:
190-
result = result + "NS: " + str(rdata.mname) + ", TECH: " + str(rdata.rname) + ", S/N: " + str(rdata.serial) + ", Refresh: " + str(rdata.refresh/60) + " min, Expire: " + str(rdata.expire/60) + " min "
190+
result = result + "NS: " + str(rdata.mname) + ", TECH: " + str(rdata.rname) + ", S/N: " + str(rdata.serial) + ", Refresh: " + str(rdata.refresh / 60) + " min, Expire: " \
191+
+ str(rdata.expire / 60) + " min "
191192
elif (type == 'CNAME') or (type == 'NS') or (type == 'PTR'):
192193
for rdata in answers:
193194
result = result + str(rdata.target) + ", "
194195
except dns.resolver.NoAnswer:
195-
result = "DNS Error while getting %s record. This could be the result of a misconfiguration in the sensor settings" % type
196+
result = "DNS Error while getting %s record. " \
197+
"This could be the result of a misconfiguration in the sensor settings" % type
196198
return result[:-2]

miniprobe/sensors/apt.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python
2-
#Copyright (c) 2014, Paessler AG <[email protected]>
3-
#All rights reserved.
4-
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
2+
# Copyright (c) 2014, Paessler AG <[email protected]>
3+
# All rights reserved.
4+
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
55
# following conditions are met:
6-
#1. Redistributions of source code must retain the above copyright notice, this list of conditions
6+
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions
77
# and the following disclaimer.
8-
#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
8+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
99
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
10-
#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
10+
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
1111
# or promote products derived from this software without specific prior written permission.
1212

13-
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
1414
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1515
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
1616
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
@@ -22,7 +22,7 @@
2222
import os
2323
import gc
2424
import logging
25-
import sys
25+
2626

2727
class APT(object):
2828
def __init__(self):
@@ -55,7 +55,6 @@ def check(self):
5555
upgrade = 0
5656
install = 0
5757
remove = 0
58-
total = 0
5958
ret = os.popen("apt-get -s dist-upgrade | grep 'newly inst'")
6059
updatedata = ret.readlines()
6160
ret.close()

miniprobe/sensors/cpuload.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python
2-
#Copyright (c) 2014, Paessler AG <[email protected]>
3-
#All rights reserved.
4-
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
2+
# Copyright (c) 2014, Paessler AG <[email protected]>
3+
# All rights reserved.
4+
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
55
# following conditions are met:
6-
#1. Redistributions of source code must retain the above copyright notice, this list of conditions
6+
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions
77
# and the following disclaimer.
8-
#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
8+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
99
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
10-
#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
10+
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
1111
# or promote products derived from this software without specific prior written permission.
1212

13-
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
1414
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1515
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
1616
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
@@ -88,20 +88,27 @@ def read_cpu(path):
8888
for line in cpu:
8989
for element in line.split(" "):
9090
data.append(element)
91-
channel_list = [{"name": "Load Average 1min",
92-
"mode": "float",
93-
"kind": "Custom",
94-
"customunit": "",
95-
"value": float(data[0])},
96-
{"name": "Load Average 5min",
97-
"mode": "float",
98-
"kind": "Custom",
99-
"customunit": "",
100-
"value": float(data[1])},
101-
{"name": "Load Average 10min",
102-
"mode": "float",
103-
"kind": "Custom",
104-
"customunit": "",
105-
"value": float(data[2])}]
91+
channel_list = [
92+
{
93+
"name": "Load Average 1min",
94+
"mode": "float",
95+
"kind": "Custom",
96+
"customunit": "",
97+
"value": float(data[0])
98+
},
99+
{
100+
"name": "Load Average 5min",
101+
"mode": "float",
102+
"kind": "Custom",
103+
"customunit": "",
104+
"value": float(data[1])
105+
},
106+
{
107+
"name": "Load Average 10min",
108+
"mode": "float",
109+
"kind": "Custom",
110+
"customunit": "",
111+
"value": float(data[2])
112+
}]
106113
cpu.close()
107114
return channel_list

miniprobe/sensors/cputemp.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
if not os.path.exists("/sys/class/thermal/thermal_zone0/temp"):
2727
temp = False
2828

29+
2930
class CPUTemp(object):
3031
def __init__(self):
3132
gc.enable()
@@ -78,7 +79,7 @@ def get_data(data, out_queue):
7879
temperature = CPUTemp()
7980
logging.debug("Running sensor: %s" % temperature.get_kind())
8081
try:
81-
temp = temperature.read_temp(data)
82+
tmp = temperature.read_temp(data)
8283
except Exception as e:
8384
logging.error("Ooops Something went wrong with '%s' sensor %s. Error: %s" % (temperature.get_kind(),
8485
data['sensorid'], e))
@@ -91,7 +92,7 @@ def get_data(data, out_queue):
9192
out_queue.put(data)
9293
return 1
9394
tempdata = []
94-
for element in temp:
95+
for element in tmp:
9596
tempdata.append(element)
9697
data = {
9798
"sensorid": int(data['sensorid']),
@@ -107,9 +108,9 @@ def get_data(data, out_queue):
107108
def read_temp(config):
108109
data = []
109110
chandata = []
110-
temp = open("/sys/class/thermal/thermal_zone0/temp", "r")
111-
lines = temp.readlines()
112-
temp.close()
111+
tmp = open("/sys/class/thermal/thermal_zone0/temp", "r")
112+
lines = tmp.readlines()
113+
tmp.close()
113114
temp_string = lines[0]
114115
logging.debug("CPUTemp Debug message: Temperature from file: %s" % temp_string)
115116
temp_c = float(temp_string) / 1000.0

0 commit comments

Comments
 (0)