mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-26 10:58:12 +00:00
Format code according to PEP0008
This commit is contained in:
parent
0f88253361
commit
5fd233c510
1 changed files with 303 additions and 263 deletions
|
@ -43,19 +43,21 @@
|
||||||
##########################################
|
##########################################
|
||||||
|
|
||||||
|
|
||||||
import logging, commands, sys, os, string
|
import commands
|
||||||
from twisted.internet import reactor, defer
|
|
||||||
from deluge.ui.client import client
|
|
||||||
from deluge.log import setupLogger
|
from deluge.log import setupLogger
|
||||||
|
from deluge.ui.client import client
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import string
|
||||||
|
import sys
|
||||||
|
from twisted.internet import reactor, defer
|
||||||
setupLogger()
|
setupLogger()
|
||||||
|
|
||||||
plugin_version = "1.0.0"
|
plugin_version = "1.0.0"
|
||||||
|
|
||||||
# create logger
|
|
||||||
log = logging.getLogger("delugeStats")
|
log = logging.getLogger("delugeStats")
|
||||||
log.setLevel(logging.WARNING)
|
log.setLevel(logging.WARNING)
|
||||||
|
|
||||||
# Deluge daemon Confs
|
|
||||||
conf = {
|
conf = {
|
||||||
'host': os.getenv('host', '127.0.0.1'),
|
'host': os.getenv('host', '127.0.0.1'),
|
||||||
'port': int(os.getenv('port', '58846')),
|
'port': int(os.getenv('port', '58846')),
|
||||||
|
@ -63,8 +65,7 @@ conf = {
|
||||||
'password': os.getenv('password', '')
|
'password': os.getenv('password', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
# names for munin :
|
names_for_munin = {
|
||||||
namesForMunin = {
|
|
||||||
'numConnections': 'numConnections',
|
'numConnections': 'numConnections',
|
||||||
'payloadUploadRate': 'payloadUploadRate',
|
'payloadUploadRate': 'payloadUploadRate',
|
||||||
'overheadUploadRate': 'overheadUploadRate',
|
'overheadUploadRate': 'overheadUploadRate',
|
||||||
|
@ -79,10 +80,17 @@ namesForMunin = {
|
||||||
'state.Other': 'other'
|
'state.Other': 'other'
|
||||||
}
|
}
|
||||||
|
|
||||||
torrentStates = [ "Downloading", "Seeding", "Paused", "Error", "Queued", "Checking", "Other" ]
|
torrent_states = ["Downloading",
|
||||||
|
"Seeding",
|
||||||
|
"Paused",
|
||||||
|
"Error",
|
||||||
|
"Queued",
|
||||||
|
"Checking",
|
||||||
|
"Other"]
|
||||||
|
|
||||||
modes = ["bandwidth", "connections", "states"]
|
modes = ["bandwidth", "connections", "states"]
|
||||||
|
|
||||||
|
|
||||||
class StatClient:
|
class StatClient:
|
||||||
|
|
||||||
def __init__(self, conf, mode):
|
def __init__(self, conf, mode):
|
||||||
|
@ -90,8 +98,8 @@ class StatClient:
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.connected = False
|
self.connected = False
|
||||||
|
|
||||||
def endSession(self, msg):
|
def end_session(self, msg):
|
||||||
log.debug("endSession : {0}".format(msg))
|
log.debug("end_session : {0}".format(msg))
|
||||||
|
|
||||||
if self.connected:
|
if self.connected:
|
||||||
log.debug("Disconnecting")
|
log.debug("Disconnecting")
|
||||||
|
@ -99,72 +107,97 @@ class StatClient:
|
||||||
|
|
||||||
reactor.stop()
|
reactor.stop()
|
||||||
|
|
||||||
def fetchInfo(self):
|
def fetch_info(self):
|
||||||
log.debug("Connecting to {0}:{1} ...".format(self.conf['host'], self.conf['port']))
|
log.debug("Connecting to {0}:{1} ...".format(
|
||||||
client.connect(self.conf['host'], self.conf['port'], self.conf['username'], self.conf['password']).addCallbacks(self.onConnectSuccess, self.endSession, errbackArgs=("Connection failed: check settings and try again."))
|
self.conf['host'], self.conf['port']))
|
||||||
|
client.connect(
|
||||||
|
self.conf['host'],
|
||||||
|
self.conf['port'],
|
||||||
|
self.conf['username'],
|
||||||
|
self.conf['password']).addCallbacks(
|
||||||
|
self.on_connect_success,
|
||||||
|
self.end_session,
|
||||||
|
errbackArgs=("Connection failed: check settings and try again."))
|
||||||
reactor.run()
|
reactor.run()
|
||||||
|
|
||||||
def onConnectSuccess(self, result):
|
def on_connect_success(self, result):
|
||||||
log.debug("Connection was successful")
|
log.debug("Connection was successful")
|
||||||
self.connected = True
|
self.connected = True
|
||||||
|
|
||||||
if self.mode == "connections":
|
if self.mode == "connections":
|
||||||
log.debug("Calling get_num_connections")
|
log.debug("Calling get_num_connections")
|
||||||
client.core.get_num_connections().addCallbacks(self.onNumConnections, self.endSession, errbackArgs=("get_num_connections failed"))
|
client.core.get_num_connections().addCallbacks(
|
||||||
|
self.on_num_connections,
|
||||||
|
self.end_session, errbackArgs=("get_num_connections failed"))
|
||||||
elif self.mode == "bandwidth":
|
elif self.mode == "bandwidth":
|
||||||
log.debug("Calling get_session_status")
|
log.debug("Calling get_session_status")
|
||||||
client.core.get_session_status(['upload_rate', 'payload_upload_rate', 'download_rate', 'payload_download_rate']).addCallbacks(self.onBandwidth, self.endSession, errbackArgs=("get_session_status faileds"))
|
interesting_status = [
|
||||||
|
'upload_rate', 'payload_upload_rate',
|
||||||
|
'download_rate', 'payload_download_rate']
|
||||||
|
client.core.get_session_status(interesting_status).addCallbacks(
|
||||||
|
self.on_bandwidth,
|
||||||
|
self.end_session,
|
||||||
|
errbackArgs=("get_session_status failed"))
|
||||||
elif self.mode == "states":
|
elif self.mode == "states":
|
||||||
log.debug("Calling get_session_state")
|
log.debug("Calling get_session_state")
|
||||||
client.core.get_session_state().addCallbacks(self.onSessionState, self.endSession, errbackArgs=("get_session_state failed"))
|
client.core.get_session_state().addCallbacks(
|
||||||
|
self.on_session_state,
|
||||||
|
self.end_session,
|
||||||
|
errbackArgs=("get_session_state failed"))
|
||||||
|
|
||||||
def onNumConnections(self, num_connections):
|
def on_num_connections(self, num_connections):
|
||||||
log.debug("Got num_connections from the daemon : {0}".format(str(num_connections)))
|
log.debug("Got num_connections from the daemon : {0}".format(
|
||||||
print "{0}.value {1}".format(namesForMunin["numConnections"], num_connections)
|
str(num_connections)))
|
||||||
self.endSession("Done")
|
print "{0}.value {1}".format(
|
||||||
|
names_for_munin["numConnections"], num_connections)
|
||||||
|
self.end_session("Done")
|
||||||
|
|
||||||
def onBandwidth(self, values):
|
def on_bandwidth(self, values):
|
||||||
log.debug("Got bandwith info from the daemon : {0}".format(str(values)))
|
log.debug(
|
||||||
|
"Got bandwidth info from the daemon : {0}".format(str(values)))
|
||||||
|
|
||||||
downloadRate = values['download_rate']
|
download_rate = values['download_rate']
|
||||||
payloadDownloadRate = values['payload_download_rate']
|
payload_download_rate = values['payload_download_rate']
|
||||||
overheadDownloadRate = downloadRate - payloadDownloadRate
|
overhead_download_rate = download_rate - payload_download_rate
|
||||||
uploadRate = values['upload_rate']
|
upload_rate = values['upload_rate']
|
||||||
payloadUploadRate = values['payload_upload_rate']
|
payload_upload_rate = values['payload_upload_rate']
|
||||||
overheadUploadRate = uploadRate - payloadUploadRate
|
overhead_upload_rate = upload_rate - payload_upload_rate
|
||||||
|
|
||||||
print "{0}.value {1}".format(namesForMunin["payloadDownloadRate"], payloadDownloadRate)
|
print "{0}.value {1}".format(
|
||||||
print "{0}.value {1}".format(namesForMunin["overheadDownloadRate"], overheadDownloadRate)
|
names_for_munin["payloadDownloadRate"], payload_download_rate)
|
||||||
print "{0}.value {1}".format(namesForMunin["payloadUploadRate"], payloadUploadRate)
|
print "{0}.value {1}".format(
|
||||||
print "{0}.value {1}".format(namesForMunin["overheadUploadRate"], overheadUploadRate)
|
names_for_munin["overheadDownloadRate"], overhead_download_rate)
|
||||||
|
print "{0}.value {1}".format(
|
||||||
|
names_for_munin["payloadUploadRate"], payload_upload_rate)
|
||||||
|
print "{0}.value {1}".format(
|
||||||
|
names_for_munin["overheadUploadRate"], overhead_upload_rate)
|
||||||
|
|
||||||
self.endSession("Done")
|
self.end_session("Done")
|
||||||
|
|
||||||
def onSessionState(self, torrentIds):
|
def on_session_state(self, torrent_ids):
|
||||||
log.debug("Got session state from the daemon")
|
log.debug("Got session state from the daemon")
|
||||||
|
|
||||||
self.states = {}
|
self.states = {}
|
||||||
for stateName in torrentStates :
|
for state_name in torrent_states:
|
||||||
self.states[stateName] = 0
|
self.states[state_name] = 0
|
||||||
#self.states={'Seeding': 0, 'Downloading': 0, 'Paused': 0, 'Error': 0, 'Queued': 0, 'Checking': 0, 'Other' : 0}
|
|
||||||
|
|
||||||
deferredList = []
|
deferred_list = []
|
||||||
for torrentId in torrentIds:
|
for torrent_id in torrent_ids:
|
||||||
log.debug(" - TorrentId : {0}".format(torrentId))
|
log.debug(" - TorrentId : {0}".format(torrent_id))
|
||||||
d = client.core.get_torrent_status(torrentId, ['state'])
|
d = client.core.get_torrent_status(torrent_id, ['state'])
|
||||||
d.addCallback(self.onOneTorrentInfo, torrentId)
|
d.addCallback(self.on_one_torrent_info, torrent_id)
|
||||||
d.addErrback(self.onOneTorrentInfoFailed, torrentId)
|
d.addErrback(self.on_one_torrent_info_failed, torrent_id)
|
||||||
deferredList.append(d)
|
deferred_list.append(d)
|
||||||
|
|
||||||
defer.DeferredList(deferredList).addCallback(self.onAllTorrentInfoFetched)
|
defer.DeferredList(deferred_list).addCallback(
|
||||||
|
self.on_all_torrent_info_fetched)
|
||||||
|
|
||||||
def onOneTorrentInfoFailed(self, torrentId):
|
def on_one_torrent_info_failed(self, torrent_id):
|
||||||
log.debug("Failed fetching torrent info {0}".format(torrentId))
|
log.debug("Failed fetching torrent info {0}".format(torrent_id))
|
||||||
self.state["Error"] = self.state["Error"] + 1;
|
self.state["Error"] = self.state["Error"] + 1
|
||||||
|
|
||||||
|
def on_one_torrent_info(self, value, torrent_id):
|
||||||
def onOneTorrentInfo(self, value, torrentId):
|
log.debug("Got torrent info : {0} -> {1}".format(torrent_id, value))
|
||||||
log.debug("Got torrent info : {0} -> {1}".format(torrentId, value))
|
|
||||||
if 'state' in value:
|
if 'state' in value:
|
||||||
state = value['state']
|
state = value['state']
|
||||||
else:
|
else:
|
||||||
|
@ -176,18 +209,19 @@ class StatClient:
|
||||||
|
|
||||||
self.states[state] = self.states[state] + 1
|
self.states[state] = self.states[state] + 1
|
||||||
|
|
||||||
def onAllTorrentInfoFetched(self, res):
|
def on_all_torrent_info_fetched(self, res):
|
||||||
log.debug("onAllTorrentInfoFetched : {0}".format(str(self.states)))
|
log.debug("on_all_torrent_info_fetched : {0}".format(str(self.states)))
|
||||||
|
|
||||||
for state in self.states:
|
for state in self.states:
|
||||||
print "{0}.value {1}".format(namesForMunin["state." + state], self.states[state])
|
print "{0}.value {1}".format(
|
||||||
|
names_for_munin["state." + state], self.states[state])
|
||||||
|
|
||||||
self.endSession("Done")
|
self.end_session("Done")
|
||||||
|
|
||||||
|
|
||||||
def getMode() :
|
def get_mode():
|
||||||
scriptName = list(os.path.split(sys.argv[0]))[1]
|
script_name = list(os.path.split(sys.argv[0]))[1]
|
||||||
mode = scriptName[string.rindex(scriptName,'_')+1:]
|
mode = script_name[string.rindex(script_name, '_') + 1:]
|
||||||
|
|
||||||
log.debug("Mode : {0}".format(str(mode)))
|
log.debug("Mode : {0}".format(str(mode)))
|
||||||
|
|
||||||
|
@ -198,17 +232,20 @@ def getMode() :
|
||||||
|
|
||||||
return mode
|
return mode
|
||||||
|
|
||||||
def printConfig(mode) :
|
|
||||||
|
def print_config(mode):
|
||||||
if mode == "connections":
|
if mode == "connections":
|
||||||
print("graph_title Number of connections")
|
print("graph_title Number of connections")
|
||||||
print("graph_args --base 1000 -l 0")
|
print("graph_args --base 1000 -l 0")
|
||||||
print("graph_vlabel connections")
|
print("graph_vlabel connections")
|
||||||
print("graph_scale yes")
|
print("graph_scale yes")
|
||||||
print("graph_category deluge")
|
print("graph_category deluge")
|
||||||
print("graph_info This graph shows the number of connections used by Deluge Torrent")
|
print(
|
||||||
print(namesForMunin["numConnections"] + ".label connections")
|
"graph_info This graph shows the number of connections used by Deluge Torrent")
|
||||||
print(namesForMunin["numConnections"] + ".min 0")
|
print(names_for_munin["numConnections"] + ".label connections")
|
||||||
print(namesForMunin["numConnections"] + ".info The number of connections used by Deluge Torrent")
|
print(names_for_munin["numConnections"] + ".min 0")
|
||||||
|
print(names_for_munin["numConnections"] +
|
||||||
|
".info The number of connections used by Deluge Torrent")
|
||||||
elif mode == "bandwidth":
|
elif mode == "bandwidth":
|
||||||
print("graph_title Bandwidth usage")
|
print("graph_title Bandwidth usage")
|
||||||
print("graph_order payloadUploadRate payloadDownloadRate overheadUploadRate overheadDownloadRate")
|
print("graph_order payloadUploadRate payloadDownloadRate overheadUploadRate overheadDownloadRate")
|
||||||
|
@ -243,11 +280,11 @@ def printConfig(mode) :
|
||||||
elif mode == "states":
|
elif mode == "states":
|
||||||
print("graph_title Torrents states")
|
print("graph_title Torrents states")
|
||||||
|
|
||||||
graphOrder = ""
|
graph_order = ""
|
||||||
for stateName in torrentStates :
|
for state_name in torrent_states:
|
||||||
graphOrder += namesForMunin["state." + stateName] + " "
|
graph_order += names_for_munin["state." + state_name] + " "
|
||||||
|
|
||||||
print("graph_order " + graphOrder)
|
print("graph_order " + graph_order)
|
||||||
print("graph_args --base 1000 -r --lower-limit 0")
|
print("graph_args --base 1000 -r --lower-limit 0")
|
||||||
print("graph_vlabel number of torrents")
|
print("graph_vlabel number of torrents")
|
||||||
print("graph_scale yes")
|
print("graph_scale yes")
|
||||||
|
@ -256,29 +293,31 @@ def printConfig(mode) :
|
||||||
print("graph_period second")
|
print("graph_period second")
|
||||||
|
|
||||||
first = True
|
first = True
|
||||||
for stateName in torrentStates :
|
for state_name in torrent_states:
|
||||||
print(namesForMunin["state." + stateName ] + ".label " + stateName)
|
print(names_for_munin["state." +
|
||||||
|
state_name] + ".label " + state_name)
|
||||||
if first:
|
if first:
|
||||||
first = False
|
first = False
|
||||||
print(namesForMunin["state." + stateName ] + ".draw AREA")
|
print(names_for_munin["state." + state_name] + ".draw AREA")
|
||||||
else:
|
else:
|
||||||
print(namesForMunin["state." + stateName ] + ".draw STACK")
|
print(names_for_munin["state." + state_name] + ".draw STACK")
|
||||||
print(namesForMunin["state." + stateName ] + ".type GAUGE")
|
print(names_for_munin["state." + state_name] + ".type GAUGE")
|
||||||
print(namesForMunin["state." + stateName ] + ".min 0")
|
print(names_for_munin["state." + state_name] + ".min 0")
|
||||||
print(namesForMunin["state." + stateName ] + ".info Number of torrents in the '" + stateName + "' state")
|
print(names_for_munin["state." + state_name] +
|
||||||
|
".info Number of torrents in the '" + state_name + "' state")
|
||||||
|
|
||||||
|
|
||||||
def fetchInfo(mode) :
|
def fetch_info(mode):
|
||||||
log.debug("Launching tests")
|
log.debug("Launching tests")
|
||||||
c = StatClient(conf, mode)
|
c = StatClient(conf, mode)
|
||||||
c.fetchInfo()
|
c.fetch_info()
|
||||||
|
|
||||||
|
|
||||||
# Parse arguments
|
# Parse arguments
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
action = sys.argv[1]
|
action = sys.argv[1]
|
||||||
if action == "config":
|
if action == "config":
|
||||||
printConfig(getMode())
|
print_config(get_mode())
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif action == "autoconf":
|
elif action == "autoconf":
|
||||||
status, output = commands.getstatusoutput('which deluged')
|
status, output = commands.getstatusoutput('which deluged')
|
||||||
|
@ -293,12 +332,13 @@ if len(sys.argv) > 1 :
|
||||||
print(mode)
|
print(mode)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif action == "version":
|
elif action == "version":
|
||||||
print('Deluge Torrent Munin plugin, version {0}'.format(plugin_version))
|
print('Deluge Torrent Munin plugin, version {0}'.format(
|
||||||
|
plugin_version))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif action != "":
|
elif action != "":
|
||||||
log.warn("Unknown argument '{0}'".format(action))
|
log.warn("Unknown argument '{0}'".format(action))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
fetchInfo(getMode())
|
fetch_info(get_mode())
|
||||||
else:
|
else:
|
||||||
fetchInfo(getMode())
|
fetch_info(get_mode())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue