1 |
#!/usr/bin/python |
---|
2 |
|
---|
3 |
# Copyright (C) 2009 Andreas Thienemann <andreas@bawue.net> |
---|
4 |
# |
---|
5 |
# This program is free software; you can redistribute it and/or modify |
---|
6 |
# it under the terms of the GNU Library General Public License as published by |
---|
7 |
# the Free Software Foundation; version 2 only |
---|
8 |
# |
---|
9 |
# This program is distributed in the hope that it will be useful, |
---|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 |
# GNU Library General Public License for more details. |
---|
13 |
# |
---|
14 |
# You should have received a copy of the GNU Library General Public License |
---|
15 |
# along with this program; if not, write to the Free Software |
---|
16 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 |
# |
---|
18 |
|
---|
19 |
# |
---|
20 |
# Munin Plugin to get job throughput for Bacula by parsing the bconsole |
---|
21 |
# output. |
---|
22 |
# |
---|
23 |
# Parameters: |
---|
24 |
# |
---|
25 |
# config (required) |
---|
26 |
# autoconf (optional - only used by munin-config) |
---|
27 |
# |
---|
28 |
|
---|
29 |
# Magic markers (optional - only used by munin-config and some |
---|
30 |
# installation scripts): |
---|
31 |
# |
---|
32 |
#%# family=contrib |
---|
33 |
#%# capabilities=autoconf |
---|
34 |
|
---|
35 |
import subprocess |
---|
36 |
import time |
---|
37 |
import sys |
---|
38 |
import re |
---|
39 |
import os |
---|
40 |
|
---|
41 |
def parse_running_jobs(): |
---|
42 |
""" Parse the bconsole output once to get the running jobs """ |
---|
43 |
|
---|
44 |
bconsole = subprocess.Popen("bconsole", stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
---|
45 |
stdout, stderr = bconsole.communicate("status\n1\nstatus\n3\n.") |
---|
46 |
|
---|
47 |
jobs = [] |
---|
48 |
clients = [] |
---|
49 |
clientlist = False |
---|
50 |
|
---|
51 |
# Hold the line numbers for devices |
---|
52 |
dev_line = [] |
---|
53 |
input = stdout.split("\n") |
---|
54 |
|
---|
55 |
for line, i in zip(input, range(0, len(input))): |
---|
56 |
if line.startswith("Connecting to Director "): |
---|
57 |
hostname = line.split()[-1].split(":")[0] |
---|
58 |
|
---|
59 |
if line.endswith(" is running"): |
---|
60 |
jobs.append(line.split()[2].split(".")[0]) |
---|
61 |
|
---|
62 |
# Parse the clientlist, warning, order of statements is important |
---|
63 |
if line.startswith("Select Client (File daemon) resource"): |
---|
64 |
clientlist = False |
---|
65 |
|
---|
66 |
if clientlist is True: |
---|
67 |
client_id, client_name = line.split() |
---|
68 |
client_clean = re.sub("^[^A-Za-z_]", "_", client_name, 1) |
---|
69 |
client_clean = re.sub("[^A-Za-z0-9_]", "_", client_clean, 0) |
---|
70 |
clients.append((client_name, client_clean, client_id[:-1])) |
---|
71 |
|
---|
72 |
if line.startswith("The defined Client resources are:"): |
---|
73 |
clientlist = True |
---|
74 |
|
---|
75 |
return hostname, jobs, clients |
---|
76 |
|
---|
77 |
|
---|
78 |
def parse(clients): |
---|
79 |
""" Parse the bconsole output """ |
---|
80 |
|
---|
81 |
query_str = "" |
---|
82 |
for client in clients: |
---|
83 |
query_str = query_str + "status\n3\n" + client[1] + "\n" |
---|
84 |
query_str = query_str + "quit" |
---|
85 |
|
---|
86 |
bconsole = subprocess.Popen("bconsole", stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
---|
87 |
stdout, stderr = bconsole.communicate(query_str) |
---|
88 |
|
---|
89 |
input = stdout.split("\n") |
---|
90 |
|
---|
91 |
jobstats = [] |
---|
92 |
|
---|
93 |
for line, pos in zip(input, range(0, len(input))): |
---|
94 |
|
---|
95 |
# Get the client name |
---|
96 |
if line.startswith("Connecting to Client "): |
---|
97 |
# client_name = input[pos].split()[3].split(".")[0] |
---|
98 |
client_name = line.split()[3] |
---|
99 |
client_clean = re.sub("^[^A-Za-z_]", "_", client_name, 1) |
---|
100 |
client_clean = re.sub("[^A-Za-z0-9_]", "_", client_clean, 0) |
---|
101 |
|
---|
102 |
# Get the current bytes |
---|
103 |
if line.endswith(" is running."): |
---|
104 |
bytes = long(input[pos+2].split()[1].split("=")[1].replace(",", "")) |
---|
105 |
jobstats.append([client_name, client_clean, bytes]) |
---|
106 |
|
---|
107 |
job_dict = {} |
---|
108 |
for job in jobstats: |
---|
109 |
job_dict[job[0].split("-")[0]] = job |
---|
110 |
|
---|
111 |
return job_dict |
---|
112 |
|
---|
113 |
|
---|
114 |
def print_config(): |
---|
115 |
hostname, jobs, clients = parse_running_jobs() |
---|
116 |
print "graph_title Bacula Job throughput" |
---|
117 |
print "graph_vlabel bytes per ${graph_period}" |
---|
118 |
print "graph_args --base 1024 -l 0" |
---|
119 |
print "graph_scale yes" |
---|
120 |
print "graph_info Bacula Job measurement." |
---|
121 |
print "graph_category Bacula" |
---|
122 |
print "graph_order", |
---|
123 |
for fd in clients: |
---|
124 |
print fd[1], |
---|
125 |
print |
---|
126 |
if os.getenv("report_hostname") is not None and \ |
---|
127 |
os.getenv("report_hostname").upper() in ["YES", "TRUE", "1", "Y"]: |
---|
128 |
print "host_name", hostname |
---|
129 |
for client in clients: |
---|
130 |
print "%s.label %s" % (client[1], client[0]) |
---|
131 |
print "%s.type DERIVE" % (client[1]) |
---|
132 |
print "%s.min 0" % (client[1]) |
---|
133 |
# print "%s.max %s" % (client[1], str(1024*1024*1024*16)) |
---|
134 |
# print "%s.cdef up,8,*" (client[1]) |
---|
135 |
sys.exit(0) |
---|
136 |
|
---|
137 |
|
---|
138 |
if "config" in sys.argv[1:]: |
---|
139 |
print_config() |
---|
140 |
elif "autoconf" in sys.argv[1:]: |
---|
141 |
for dir in os.getenv("PATH").split(":"): |
---|
142 |
for root, dirs, files in os.walk(dir): |
---|
143 |
if "bconsole" in files: |
---|
144 |
print "yes" |
---|
145 |
sys.exit(0) |
---|
146 |
print "no" |
---|
147 |
sys.exit(1) |
---|
148 |
elif "suggest" in sys.argv[1:]: |
---|
149 |
sys.exit(1) |
---|
150 |
else: |
---|
151 |
hostname, jobs, clients = parse_running_jobs() |
---|
152 |
str = [] |
---|
153 |
for client in clients: |
---|
154 |
if client[0].split("-")[0] in jobs: |
---|
155 |
str.append((client[0], client[2])) |
---|
156 |
|
---|
157 |
client_values = parse(str) |
---|
158 |
|
---|
159 |
for client in clients: |
---|
160 |
client_name_short = client[0].split("-")[0] |
---|
161 |
if client_name_short in client_values: |
---|
162 |
print "%s.value %s" % (client_values[client_name_short][1], client_values[client_name_short][2]) |
---|
163 |
else: |
---|
164 |
print "%s.value %s" % (client[1], "0") |
---|
165 |
|
---|
166 |
sys.exit(0) |
---|