Skip to content

Commit 8b6b4ef

Browse files
authored
Merge pull request mvonthron#15 from exhuma/fix-global-plugin-config
Fix global plugin config
2 parents 26d188a + 5200b79 commit 8b6b4ef

File tree

8 files changed

+112
-95
lines changed

8 files changed

+112
-95
lines changed

bin/fetch.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from __future__ import print_function
23
import pwd
34
import json
45
import os
@@ -50,7 +51,7 @@ def pack_values(config, values):
5051
else:
5152
age = (date - int(latest_date)) // (24*3600)
5253
if age < 7:
53-
print "{0} Not found measurement {1} (updated {2} days ago)".format(Symbol.WARN_YELLOW, name, age)
54+
print("{0} Not found measurement {1} (updated {2} days ago)".format(Symbol.WARN_YELLOW, name, age))
5455
# otherwise very probably a removed plugin, no problem
5556

5657
return [{
@@ -69,7 +70,7 @@ def main(config_filename=Defaults.FETCH_CONFIG):
6970
config = None
7071
with open(config_filename) as f:
7172
config = json.load(f)
72-
print "{0} Opened configuration: {1}".format(Symbol.OK_GREEN, f.name)
73+
print("{0} Opened configuration: {1}".format(Symbol.OK_GREEN, f.name))
7374
assert config
7475

7576
client = influxdb.InfluxDBClient(config['influxdb']['host'],
@@ -80,7 +81,7 @@ def main(config_filename=Defaults.FETCH_CONFIG):
8081
try:
8182
client.get_list_database()
8283
except influxdb.client.InfluxDBClientError as e:
83-
print " {0} Could not connect to database: {1}".format(Symbol.WARN_YELLOW, e.message)
84+
print(" {0} Could not connect to database: {1}".format(Symbol.WARN_YELLOW, e))
8485
sys.exit(1)
8586
else:
8687
client.switch_database(config['influxdb']['database'])
@@ -90,31 +91,31 @@ def main(config_filename=Defaults.FETCH_CONFIG):
9091
values = read_state_file(statefile)
9192

9293
except Exception as e:
93-
print "{0} Could not read state file {1}: {2}".format(Symbol.NOK_RED, statefile, e.message)
94+
print("{0} Could not read state file {1}: {2}".format(Symbol.NOK_RED, statefile, e))
9495
continue
9596
else:
96-
print "{0} Parsed: {1}".format(Symbol.OK_GREEN, statefile)
97+
print("{0} Parsed: {1}".format(Symbol.OK_GREEN, statefile))
9798

9899
data = pack_values(config, values)
99100
if len(data):
100-
# print data
101+
# print(data)
101102
try:
102103
client.write_points(data, time_precision='s')
103104
except influxdb.client.InfluxDBClientError as e:
104-
print " {0} Could not write data to database: {1}".format(Symbol.WARN_YELLOW, e.message)
105+
print(" {0} Could not write data to database: {1}".format(Symbol.WARN_YELLOW, e))
105106
else:
106107
config['lastupdate'] = max(config['lastupdate'], int(values[1]))
107-
print "{0} Successfully written {1} new measurements".format(Symbol.OK_GREEN, len(data))
108+
print("{0} Successfully written {1} new measurements".format(Symbol.OK_GREEN, len(data)))
108109
else:
109-
print Symbol.NOK_RED, "No data found, is Munin still running?"
110+
print("%s No data found, is Munin still running?", Symbol.NOK_RED)
110111

111112
with open(config_filename, "w") as f:
112113
json.dump(config, f)
113-
print "{0} Updated configuration: {1}".format(Symbol.OK_GREEN, f.name)
114+
print("{0} Updated configuration: {1}".format(Symbol.OK_GREEN, f.name))
114115

115116
def uninstall_cron():
116117
if os.geteuid() != 0:
117-
print "It seems you are not root, please run \"muninflux fetch --uninstall-cron\" again with root privileges".format(sys.argv[0])
118+
print("It seems you are not root, please run \"muninflux fetch --uninstall-cron\" again with root privileges".format(sys.argv[0]))
118119
sys.exit(1)
119120

120121
try:
@@ -131,7 +132,7 @@ def uninstall_cron():
131132

132133
def install_cron(script_file, period):
133134
if os.geteuid() != 0:
134-
print "It seems you are not root, please run \"muninflux fetch --install-cron\" again with root privileges".format(sys.argv[0])
135+
print("It seems you are not root, please run \"muninflux fetch --install-cron\" again with root privileges".format(sys.argv[0]))
135136
sys.exit(1)
136137

137138
try:
@@ -167,13 +168,13 @@ def install_cron(script_file, period):
167168

168169
if args.script_path:
169170
install_cron(args.script_path, args.period)
170-
print "{0} Cron job installed for user {1}".format(Symbol.OK_GREEN, CRON_USER)
171+
print("{0} Cron job installed for user {1}".format(Symbol.OK_GREEN, CRON_USER))
171172
elif args.uninstall_cron:
172173
nb = uninstall_cron()
173174
if nb:
174-
print "{0} Cron job uninstalled for user {1} ({2} entries deleted)".format(Symbol.OK_GREEN, CRON_USER, nb)
175+
print("{0} Cron job uninstalled for user {1} ({2} entries deleted)".format(Symbol.OK_GREEN, CRON_USER, nb))
175176
else:
176-
print "No matching job found (searching comment \"{1}\" in crontab for user {2})".format(Symbol.WARN_YELLOW,
177-
CRON_COMMENT, CRON_USER)
177+
print("No matching job found (searching comment \"{1}\" in crontab for user {2})".format(Symbol.WARN_YELLOW,
178+
CRON_COMMENT, CRON_USER))
178179
else:
179180
main(args.config)

bin/import.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from __future__ import print_function
23

34
import argparse
45
import sys
@@ -14,42 +15,42 @@
1415
def retrieve_munin_configuration(settings):
1516
"""
1617
"""
17-
print "Exploring Munin structure"
18+
print("Exploring Munin structure")
1819

1920
try:
2021
settings = munin.discover_from_datafile(settings)
2122
except Exception as e:
22-
print " {0} Could not process datafile ({1}), will read www and RRD cache instead".format(Symbol.NOK_RED, settings.paths['datafile'])
23+
print(" {0} Could not process datafile ({1}), will read www and RRD cache instead".format(Symbol.NOK_RED, settings.paths['datafile']))
2324

2425
# read /var/cache/munin/www to check what's currently displayed on the dashboard
2526
settings = munin.discover_from_www(settings)
2627
settings = rrd.discover_from_rrd(settings, insert_missing=False)
2728
else:
28-
print " {0} Found {1}: extracted {2} measurement units".format(Symbol.OK_GREEN, settings.paths['datafile'],
29-
settings.nb_fields)
29+
print(" {0} Found {1}: extracted {2} measurement units".format(Symbol.OK_GREEN, settings.paths['datafile'],
30+
settings.nb_fields))
3031

3132
# for each host, find the /var/lib/munin/<host> directory and check if node name and plugin conf match RRD files
3233
try:
3334
rrd.check_rrd_files(settings)
3435
except Exception as e:
35-
print " {0} {1}".format(Symbol.NOK_RED, e.message)
36+
print(" {0} {1}".format(Symbol.NOK_RED, e))
3637
else:
37-
print " {0} Found {1} RRD files".format(Symbol.OK_GREEN, settings.nb_rrd_files)
38+
print(" {0} Found {1} RRD files".format(Symbol.OK_GREEN, settings.nb_rrd_files))
3839

3940
return settings
4041

4142

4243
def main(args):
43-
print "{0}Munin to InfluxDB migration tool{1}".format(Color.BOLD, Color.CLEAR)
44-
print "-" * 20
44+
print("{0}Munin to InfluxDB migration tool{1}".format(Color.BOLD, Color.CLEAR))
45+
print("-" * 20)
4546

4647
settings = Settings(args)
4748
settings = retrieve_munin_configuration(settings)
4849

4950
# export RRD files as XML for (much) easier parsing (but takes much more time)
50-
print "\nExporting RRD databases:".format(settings.nb_rrd_files)
51+
print("\nExporting RRD databases:".format(settings.nb_rrd_files))
5152
nb_xml = rrd.export_to_xml(settings)
52-
print " {0} Exported {1} RRD files to XML ({2})".format(Symbol.OK_GREEN, nb_xml, settings.paths['xml'])
53+
print(" {0} Exported {1} RRD files to XML ({2})".format(Symbol.OK_GREEN, nb_xml, settings.paths['xml']))
5354

5455
#reads every XML file and export as in the InfluxDB database
5556
exporter = InfluxdbClient(settings)
@@ -65,17 +66,17 @@ def main(args):
6566
exporter.import_from_xml()
6667

6768
settings = exporter.get_settings()
68-
print "{0} Munin data successfully imported to {1}/db/{2}".format(Symbol.OK_GREEN, settings.influxdb['host'],
69-
settings.influxdb['database'])
69+
print("{0} Munin data successfully imported to {1}/db/{2}".format(Symbol.OK_GREEN, settings.influxdb['host'],
70+
settings.influxdb['database']))
7071

7172
settings.save_fetch_config()
72-
print "{0} Configuration for 'munin-influxdb fetch' exported to {1}".format(Symbol.OK_GREEN,
73-
settings.paths['fetch_config'])
73+
print("{0} Configuration for 'munin-influxdb fetch' exported to {1}".format(Symbol.OK_GREEN,
74+
settings.paths['fetch_config']))
7475

7576
# Generate a JSON file to be uploaded to Grafana
76-
print "\n{0}Grafaba dashboard{1}".format(Color.BOLD, Color.CLEAR)
77+
print("\n{0}Grafaba dashboard{1}".format(Color.BOLD, Color.CLEAR))
7778
if not settings.influxdb['group_fields'] and settings.grafana['create']:
78-
print Symbol.NOK_RED, "Grafana dashboard generation is only supported in grouped fields mode."
79+
print("%s Grafana dashboard generation is only supported in grouped fields mode.", Symbol.NOK_RED)
7980
return
8081

8182
if settings.interactive:
@@ -92,19 +93,19 @@ def main(args):
9293
try:
9394
dash_url = dashboard.upload()
9495
except Exception as e:
95-
print "{0} Didn't quite work uploading: {1}".format(Symbol.NOK_RED, e.message)
96+
print("{0} Didn't quite work uploading: {1}".format(Symbol.NOK_RED, e))
9697
else:
97-
print "{0} A Grafana dashboard has been successfully uploaded to {1}".format(Symbol.OK_GREEN, dash_url)
98+
print("{0} A Grafana dashboard has been successfully uploaded to {1}".format(Symbol.OK_GREEN, dash_url))
9899

99100
if settings.grafana['filename']:
100101
try:
101102
dashboard.save()
102103
except Exception as e:
103-
print "{0} Could not write Grafana dashboard: {1}".format(Symbol.NOK_RED, e.message)
104+
print("{0} Could not write Grafana dashboard: {1}".format(Symbol.NOK_RED, e))
104105
else:
105-
print "{0} A Grafana dashboard has been successfully generated to {1}".format(Symbol.OK_GREEN, settings.grafana['filename'])
106+
print("{0} A Grafana dashboard has been successfully generated to {1}".format(Symbol.OK_GREEN, settings.grafana['filename']))
106107
else:
107-
print "Then we're good! Have a nice day!"
108+
print("Then we're good! Have a nice day!")
108109

109110

110111
if __name__ == "__main__":
@@ -172,8 +173,8 @@ def main(args):
172173
try:
173174
main(args)
174175
except KeyboardInterrupt:
175-
print "\n{0} Canceled.".format(Symbol.NOK_RED)
176+
print("\n{0} Canceled.".format(Symbol.NOK_RED))
176177
sys.exit(1)
177178
except Exception as e:
178-
print "{0} Error: {1}".format(Symbol.NOK_RED, e.message)
179-
sys.exit(1)
179+
print("{0} Error: {1}".format(Symbol.NOK_RED, e))
180+
sys.exit(1)

munininfluxdb/grafana.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import print_function
2+
13
import json
24
import urlparse
35

@@ -123,7 +125,10 @@ def process_graph_types(self, fields):
123125
# LINE* should be matched *but not* LINESTACK*
124126
if hasStack and draw.startswith("LINE") and not draw.startswith("LINESTACK"):
125127
current["stack"] = False
126-
current["linewidth"] = int(draw[-1])/2 # lines appear bigger on Grafana
128+
if draw == 'LINE':
129+
current["linewidth"] = 1
130+
else:
131+
current["linewidth"] = int(draw[-1])/2 # lines appear bigger on Grafana
127132

128133
if len(current) > 1:
129134
self.overrides.append(current)
@@ -219,13 +224,13 @@ def __init__(self, settings):
219224

220225
def prompt_setup(self):
221226
setup = self.settings.grafana
222-
print "\nGrafana: Please enter your connection information"
227+
print("\nGrafana: Please enter your connection information")
223228
setup['host'] = raw_input(" - host [http://localhost:3000]: ").strip() or "http://localhost:3000"
224229
setup['auth'] = None
225230
setup['filename'] = None
226231

227232
while not GrafanaApi.test_host(setup['host']) and not setup['filename']:
228-
print "\n{0}We couldn't connect to {1}, please try again or leave empty to save to a local file{2}".format(Symbol.WARN_YELLOW, setup['host'], Color.CLEAR)
233+
print("\n{0}We couldn't connect to {1}, please try again or leave empty to save to a local file{2}".format(Symbol.WARN_YELLOW, setup['host'], Color.CLEAR))
229234
setup['host'] = raw_input(" - host: ").strip() or ""
230235
if not setup['host']:
231236
setup['filename'] = raw_input(" - local file [/tmp/munin-grafana.json]: ").strip() or "/tmp/munin-grafana.json"
@@ -372,7 +377,7 @@ def create_dashboard(self, dashboardJson):
372377
if r.ok:
373378
return "".join([self.host, "/dashboard/db/", r.json()['slug']])
374379
else:
375-
print r.json()
380+
print(r.json())
376381
r.raise_for_status()
377382

378383

@@ -392,7 +397,7 @@ def create_dashboard(self, dashboardJson):
392397

393398
# pprint(dashboard.to_json())
394399

395-
print json.dumps(dashboard.to_json(),indent=2, separators=(',', ': '))
400+
print(json.dumps(dashboard.to_json(),indent=2, separators=(',', ': ')))
396401

397402
# ---
398403

@@ -410,4 +415,4 @@ def create_dashboard(self, dashboardJson):
410415
#
411416
# dashboard = Dashboard("Munin dashboard")
412417
# dashboard.generate(conf)
413-
# print json.dumps(dashboard.to_json(),indent=2, separators=(',', ': '))
418+
# print(json.dumps(dashboard.to_json(),indent=2, separators=(',', ': ')))

0 commit comments

Comments
 (0)