2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Plugin bacula_job: various improvements, flake8-clean

* avoid hiding builtins with variable names (input, bytes, str)
* return exitcode=0 for autoconf "no"
* unify indentation
* fix all issues reported by flake8
This commit is contained in:
Lars Kruse 2018-03-27 04:02:37 +02:00
parent 3533998c5b
commit fa896dffaa

View File

@ -22,145 +22,149 @@
# #
# Parameters: # Parameters:
# #
# config (required) # config (required)
# autoconf (optional - only used by munin-config) # autoconf (optional - only used by munin-config)
# #
# Magic markers (optional - only used by munin-config and some # Magic markers (optional - only used by munin-config and some
# installation scripts): # installation scripts):
# #
#%# family=contrib # #%# family=contrib
#%# capabilities=autoconf # #%# capabilities=autoconf
import subprocess import subprocess
import time
import sys import sys
import re import re
import os import os
def parse_running_jobs(): def parse_running_jobs():
""" Parse the bconsole output once to get the running jobs """ """ Parse the bconsole output once to get the running jobs """
bconsole = subprocess.Popen("bconsole", stdin=subprocess.PIPE, stdout=subprocess.PIPE) bconsole = subprocess.Popen("bconsole", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = bconsole.communicate("status\n1\nstatus\n3\n.") stdout, stderr = bconsole.communicate("status\n1\nstatus\n3\n.")
jobs = [] jobs = []
clients = [] clients = []
clientlist = False clientlist = False
# Hold the line numbers for devices # Hold the line numbers for devices
dev_line = [] input_lines = stdout.split("\n")
input = stdout.split("\n")
for line, i in zip(input, range(0, len(input))): for line, i in zip(input_lines, range(0, len(input_lines))):
if line.startswith("Connecting to Director "): if line.startswith("Connecting to Director "):
hostname = line.split()[-1].split(":")[0] hostname = line.split()[-1].split(":")[0]
if line.endswith(" is running"): if line.endswith(" is running"):
jobs.append(line.split()[2].split(".")[0]) jobs.append(line.split()[2].split(".")[0])
# Parse the clientlist, warning, order of statements is important # Parse the clientlist, warning, order of statements is important
if line.startswith("Select Client (File daemon) resource"): if line.startswith("Select Client (File daemon) resource"):
clientlist = False clientlist = False
if clientlist is True: if clientlist is True:
client_id, client_name = line.split() client_id, client_name = line.split()
client_clean = re.sub("^[^A-Za-z_]", "_", client_name, 1) client_clean = re.sub("^[^A-Za-z_]", "_", client_name, 1)
client_clean = re.sub("[^A-Za-z0-9_]", "_", client_clean, 0) client_clean = re.sub("[^A-Za-z0-9_]", "_", client_clean, 0)
clients.append((client_name, client_clean, client_id[:-1])) clients.append((client_name, client_clean, client_id[:-1]))
if line.startswith("The defined Client resources are:"): if line.startswith("The defined Client resources are:"):
clientlist = True clientlist = True
return hostname, jobs, clients return hostname, jobs, clients
def parse(clients): def parse(clients):
""" Parse the bconsole output """ """ Parse the bconsole output """
query_str = "" query_str = ""
for client in clients: for client in clients:
query_str = query_str + "status\n3\n" + client[1] + "\n" query_str = query_str + "status\n3\n" + client[1] + "\n"
query_str = query_str + "quit" query_str = query_str + "quit"
bconsole = subprocess.Popen("bconsole", stdin=subprocess.PIPE, stdout=subprocess.PIPE) bconsole = subprocess.Popen("bconsole", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = bconsole.communicate(query_str) stdout, stderr = bconsole.communicate(query_str)
input = stdout.split("\n") input_lines = stdout.split("\n")
jobstats = [] jobstats = []
for line, pos in zip(input, range(0, len(input))): for line, pos in zip(input_lines, range(0, len(input_lines))):
# Get the client name # Get the client name
if line.startswith("Connecting to Client "): if line.startswith("Connecting to Client "):
# client_name = input[pos].split()[3].split(".")[0] # client_name = input_lines[pos].split()[3].split(".")[0]
client_name = line.split()[3] client_name = line.split()[3]
client_clean = re.sub("^[^A-Za-z_]", "_", client_name, 1) client_clean = re.sub("^[^A-Za-z_]", "_", client_name, 1)
client_clean = re.sub("[^A-Za-z0-9_]", "_", client_clean, 0) client_clean = re.sub("[^A-Za-z0-9_]", "_", client_clean, 0)
# Get the current bytes # Get the current bytes
if line.endswith(" is running."): if line.endswith(" is running."):
bytes = long(input[pos+2].split()[1].split("=")[1].replace(",", "")) bytes_count_text = input_lines[pos+2].split()[1].split("=")[1].replace(",", "")
jobstats.append([client_name, client_clean, bytes]) try:
# python2
bytes_count = long(bytes_count_text)
except NameError:
# python3
bytes_count = int(bytes_count_text)
jobstats.append([client_name, client_clean, bytes_count])
job_dict = {} job_dict = {}
for job in jobstats: for job in jobstats:
job_dict[job[0].split("-")[0]] = job job_dict[job[0].split("-")[0]] = job
return job_dict return job_dict
def print_config(): def print_config():
hostname, jobs, clients = parse_running_jobs() hostname, jobs, clients = parse_running_jobs()
print "graph_title Bacula Job throughput" print("graph_title Bacula Job throughput")
print "graph_vlabel bytes per ${graph_period}" print("graph_vlabel bytes per ${graph_period}")
print "graph_args --base 1024 -l 0" print("graph_args --base 1024 -l 0")
print "graph_scale yes" print("graph_scale yes")
print "graph_info Bacula Job measurement." print("graph_info Bacula Job measurement.")
print "graph_category backup" print("graph_category backup")
print "graph_order", print("graph_order", " ".join(fd[1] for fd in clients))
for fd in clients: print()
print fd[1], if ((os.getenv("report_hostname") is not None) and
print (os.getenv("report_hostname").upper() in ["YES", "TRUE", "1", "Y"])):
if os.getenv("report_hostname") is not None and \ print("host_name", hostname)
os.getenv("report_hostname").upper() in ["YES", "TRUE", "1", "Y"]: for client in clients:
print "host_name", hostname print("%s.label %s" % (client[1], client[0]))
for client in clients: print("%s.type DERIVE" % (client[1]))
print "%s.label %s" % (client[1], client[0]) print("%s.min 0" % (client[1]))
print "%s.type DERIVE" % (client[1]) # print("%s.max %s" % (client[1], str(1024*1024*1024*16)))
print "%s.min 0" % (client[1]) # print("%s.cdef up,8,*" (client[1]))
# print "%s.max %s" % (client[1], str(1024*1024*1024*16)) sys.exit(0)
# print "%s.cdef up,8,*" (client[1])
sys.exit(0)
if "config" in sys.argv[1:]: if "config" in sys.argv[1:]:
print_config() print_config()
elif "autoconf" in sys.argv[1:]: elif "autoconf" in sys.argv[1:]:
for dir in os.getenv("PATH").split(":"): for directory in os.getenv("PATH").split(":"):
for root, dirs, files in os.walk(dir): for root, dirs, files in os.walk(directory):
if "bconsole" in files: if "bconsole" in files:
print "yes" print("yes")
sys.exit(0) sys.exit(0)
print "no" print("no")
sys.exit(1) sys.exit(0)
elif "suggest" in sys.argv[1:]: elif "suggest" in sys.argv[1:]:
sys.exit(1) sys.exit(1)
else: else:
hostname, jobs, clients = parse_running_jobs() hostname, jobs, clients = parse_running_jobs()
str = [] client_pairs = []
for client in clients: for client in clients:
if client[0].split("-")[0] in jobs: if client[0].split("-")[0] in jobs:
str.append((client[0], client[2])) client_pairs.append((client[0], client[2]))
client_values = parse(str) client_values = parse(client_pairs)
for client in clients: for client in clients:
client_name_short = client[0].split("-")[0] client_name_short = client[0].split("-")[0]
if client_name_short in client_values: if client_name_short in client_values:
print "%s.value %s" % (client_values[client_name_short][1], client_values[client_name_short][2]) print("%s.value %s" % (client_values[client_name_short][1],
else: client_values[client_name_short][2]))
print "%s.value %s" % (client[1], "0") else:
print("%s.value %s" % (client[1], "0"))
sys.exit(0) sys.exit(0)