ovirt-node 2.2.0 release
[ovirt-node.git] / scripts / collectd.py
blob1a4173b15ad346816dd7c56bdc4c59838e9d3de2
1 #!/usr/bin/python
3 # Configures the collectd daemon.
5 import os
6 import sys
7 from ovirtnode.ovirtfunctions import *
8 from subprocess import Popen, PIPE, STDOUT
9 from snack import *
10 import _snack
12 collectd_conf="/etc/collectd.conf"
14 def write_collectd_config(server, port):
15 if os.path.exists(collectd_conf + ".in"):
16 sed_cmd = "sed -e \"s/@COLLECTD_SERVER@/%s/\" \
17 -e \"s/@COLLECTD_PORT@/%s/\" %s.in \
18 > %s" % (server, port, collectd_conf, collectd_conf)
19 sed = subprocess.Popen(sed_cmd, shell=True, stdout=PIPE, stderr=STDOUT)
20 os.system("chkconfig collectd on &> /dev/null")
21 os.system("service collectd restart &> /dev/null")
22 return True
24 def get_collectd_config():
25 collectd_config = open(collectd_conf)
26 try:
27 for line in collectd_config:
28 if "Server" in line:
29 start, config = line.split("Server ", 2)
30 server, port = config.split()
31 server = server.strip('"')
32 return (server,port)
33 except:
34 return
35 finally:
36 collectd_config.close()
38 # AUTO for auto-install
39 def collectd_auto():
40 host = ""
41 port = ""
42 if not OVIRT_VARS.has_key("OVIRT_COLLECTD_SERVER") or not OVIRT_VARS.has_key("OVIRT_COLLECTD_PORT"):
43 logger.info("Attempting to locate remote collectd server...")
44 try:
45 host, port = find_srv("collectd", "udp")
46 except:
47 pass
48 if not host is "" and not port is "":
49 logger.info("collectd server found! Using collectd server " + host + ":" + port)
50 write_collectd_config(host, port)
51 return True
52 else:
53 logger.error("collectd server not found!")
54 return False
55 else:
56 logger.info("Using default collectd server: " + OVIRT_VARS["OVIRT_COLLECTD_SERVER"]+ " : " + OVIRT_VARS["OVIRT_COLLECTD_PORT"])
57 write_collectd_config(OVIRT_VARS["OVIRT_COLLECTD_SERVER"], OVIRT_VARS["OVIRT_COLLECTD_PORT"])
58 return True
61 # configuration UI plugin interface
63 class Plugin(PluginBase):
64 """Plugin for Monitoring(collectd) configuration option.
65 """
67 def __init__(self, ncs):
68 PluginBase.__init__(self, "Monitoring(collectd)", ncs)
70 def form(self):
71 elements = Grid(2, 10)
72 elements.setField(Label("Monitoring(collectd) Configuration"), 0, 0, anchorLeft = 1)
73 elements.setField(Label(""), 0, 1, anchorLeft = 1)
74 elements.setField(Label("Collectd"), 0, 2, anchorLeft = 1)
75 elements.setField(Textbox(45,3,"Collectd gathers statistics about the system that\ncan be used to find performance bottlenecks\nand predict future system load."), 0, 3, anchorLeft = 1)
76 collectd_grid = Grid(2,2)
77 collectd_grid.setField(Label("Server Address:"), 0, 0, anchorLeft = 1)
78 self.collectd_server = Entry(20, "")
79 self.collectd_server.setCallback(self.valid_collectd_server_callback)
80 collectd_grid.setField(self.collectd_server, 1, 0, anchorLeft = 1, padding=(2, 0, 0, 1))
81 self.collectd_port = Entry(5, "")
82 self.collectd_port.setCallback(self.valid_collectd_port_callback)
83 collectd_grid.setField(Label("Server Port:"), 0, 1, anchorLeft = 1)
84 collectd_grid.setField(self.collectd_port, 1, 1, anchorLeft = 1, padding=(2, 0, 0, 1))
85 elements.setField(collectd_grid, 0, 4, anchorLeft = 1, padding = (0,1,0,0))
86 collectd_config = get_collectd_config()
87 if not collectd_config is None:
88 collectd_server, collectd_port = get_collectd_config()
89 self.collectd_server.set(collectd_server)
90 self.collectd_port.set(collectd_port)
91 else:
92 self.collectd_port.set("7634")
93 return [Label(""), elements]
95 def action(self):
96 self.ncs.screen.setColor("BUTTON", "black", "red")
97 self.ncs.screen.setColor("ACTBUTTON", "blue", "white")
98 if len(self.collectd_server.value()) > 0 and len(self.collectd_port.value()) > 0 :
99 if write_collectd_config(self.collectd_server.value(), self.collectd_port.value()):
100 ButtonChoiceWindow(self.ncs.screen, "Collectd Configuration", "Collectd Configuration Successfully Changed", buttons = ['Ok'])
101 self.ncs.reset_screen_colors()
102 return True
103 else:
104 ButtonChoiceWindow(self.ncs.screen, "Collectd Configuration", "Collectd Configuration Failed", buttons = ['Ok'])
105 self.ncs.reset_screen_colors()
106 return False
108 def valid_collectd_server_callback(self):
109 if not is_valid_host_or_ip(self.collectd_server.value()):
110 self.ncs.screen.setColor("BUTTON", "black", "red")
111 self.ncs.screen.setColor("ACTBUTTON", "blue", "white")
112 ButtonChoiceWindow(self.ncs.screen, "Configuration Check", "Invalid Hostname or Address", buttons = ['Ok'])
113 self.ncs.reset_screen_colors()
115 def valid_collectd_port_callback(self):
116 if not is_valid_port(self.collectd_port.value()):
117 self.ncs.screen.setColor("BUTTON", "black", "red")
118 self.ncs.screen.setColor("ACTBUTTON", "blue", "white")
119 ButtonChoiceWindow(self.ncs.screen, "Configuration Check", "Invalid Port Number", buttons = ['Ok'])
120 self.ncs.reset_screen_colors()
122 def get_plugin(ncs):
123 return Plugin(ncs)