|
12 | 12 | import protobix |
13 | 13 |
|
14 | 14 |
|
15 | | -class SupervisorServer(object): |
| 15 | +class SupervisorServer(protobix.SampleProbe): |
16 | 16 |
|
17 | | - __version__ = '0.0.8' |
18 | | - ZBX_CONN_ERR = 'ERR - unable to send data to Zabbix [%s]' |
| 17 | + __version__ = '0.0.9' |
19 | 18 |
|
20 | 19 | SUPERV_STAT_CHECK='sudo supervisorctl status' |
21 | 20 | SUPERV_STATES = { |
@@ -56,6 +55,17 @@ def _get_infos(self): |
56 | 55 | worker_list[group_name][proc_name][proc_status] += 1 |
57 | 56 | return worker_list |
58 | 57 |
|
| 58 | + def _parse_args(self): |
| 59 | + # Parse the script arguments |
| 60 | + # Common part |
| 61 | + parser = super( SupervisorServer, self)._parse_args() |
| 62 | + |
| 63 | + (options, args) = parser.parse_args() |
| 64 | + return (options, args) |
| 65 | + |
| 66 | + def _init_probe(self): |
| 67 | + self.hostname = socket.getfqdn() |
| 68 | + |
59 | 69 | def _get_metrics(self): |
60 | 70 | data = {} |
61 | 71 | try: |
@@ -85,103 +95,6 @@ def _get_discovery(self): |
85 | 95 | raise Exception('Fail to get supervisord infos') |
86 | 96 | return {'supervisord.workers.discovery': data} |
87 | 97 |
|
88 | | - def _parse_args(self): |
89 | | - ''' Parse the script arguments |
90 | | - ''' |
91 | | - parser = optparse.OptionParser() |
92 | | - |
93 | | - parser.add_option("-d", "--dry", action="store_true", |
94 | | - help="Performs Supervisord calls but do not send " |
95 | | - "anything to the Zabbix server. Can be used " |
96 | | - "for both Update & Discovery mode") |
97 | | - parser.add_option("-D", "--debug", action="store_true", |
98 | | - help="Enable debug mode. This will prevent bulk send " |
99 | | - "operations and force sending items one after the " |
100 | | - "other, displaying result for each one") |
101 | | - parser.add_option("-v", "--verbose", action="store_true", |
102 | | - help="When used with debug option, will force value " |
103 | | - "display for each items managed. Beware that it " |
104 | | - "can be pretty too much verbose, specialy for LLD") |
105 | | - |
106 | | - mode_group = optparse.OptionGroup(parser, "Program Mode") |
107 | | - mode_group.add_option("--update-items", action="store_const", |
108 | | - dest="mode", const="update_items", |
109 | | - help="Get & send items to Zabbix. This is the default " |
110 | | - "behaviour even if option is not specified") |
111 | | - mode_group.add_option("--discovery", action="store_const", |
112 | | - dest="mode", const="discovery", |
113 | | - help="If specified, will perform Zabbix Low Level " |
114 | | - "Discovery on Supervisord. " |
115 | | - "Default is to get & send items") |
116 | | - parser.add_option_group(mode_group) |
117 | | - parser.set_defaults(mode="update_items") |
118 | | - |
119 | | - general_options = optparse.OptionGroup(parser, "Supervisord " |
120 | | - "configuration options") |
121 | | - |
122 | | - parser.add_option_group(general_options) |
123 | | - |
124 | | - zabbix_options = optparse.OptionGroup(parser, "Zabbix configuration") |
125 | | - zabbix_options.add_option("--zabbix-server", metavar="HOST", default="localhost", |
126 | | - help="The hostname of Zabbix server or " |
127 | | - "proxy, default is localhost.") |
128 | | - zabbix_options.add_option("--zabbix-port", metavar="PORT", default=10051, |
129 | | - help="The port on which the Zabbix server or " |
130 | | - "proxy is running, default is 10051.") |
131 | | - parser.add_option_group(zabbix_options) |
132 | | - |
133 | | - return parser.parse_args() |
134 | | - |
135 | | - def _init_container(self): |
136 | | - zbx_container = protobix.DataContainer( |
137 | | - data_type = 'items', |
138 | | - zbx_host = self.options.zabbix_server, |
139 | | - zbx_port = int(self.options.zabbix_port), |
140 | | - debug = self.options.debug, |
141 | | - dryrun = self.options.dry |
142 | | - ) |
143 | | - return zbx_container |
144 | | - |
145 | | - def run(self): |
146 | | - (self.options, args) = self._parse_args() |
147 | | - hostname = socket.getfqdn() |
148 | | - |
149 | | - # Step 1: init container |
150 | | - try: |
151 | | - zbx_container = self._init_container() |
152 | | - except: |
153 | | - return 1 |
154 | | - |
155 | | - # Step 2: get data |
156 | | - try: |
157 | | - data = {} |
158 | | - if self.options.mode == "update_items": |
159 | | - zbx_container.set_type("items") |
160 | | - data[hostname] = self._get_metrics() |
161 | | - data[hostname]['supervisord.zbx_version'] = self.__version__ |
162 | | - |
163 | | - elif self.options.mode == "discovery": |
164 | | - zbx_container.set_type("lld") |
165 | | - data[hostname] = self._get_discovery() |
166 | | - except: |
167 | | - return 2 |
168 | | - |
169 | | - # Step 3: format & load data into container |
170 | | - try: |
171 | | - zbx_container.add(data) |
172 | | - except: |
173 | | - return 3 |
174 | | - |
175 | | - # Step 4: send container data to Zabbix server |
176 | | - try: |
177 | | - zbx_container.send(zbx_container) |
178 | | - except protobix.SenderException as zbx_e: |
179 | | - if self.options.debug: |
180 | | - print self.ZBX_CONN_ERR % zbx_e.err_text |
181 | | - return 4 |
182 | | - # Everything went fine. Let's return 0 and exit |
183 | | - return 0 |
184 | | - |
185 | 98 | if __name__ == '__main__': |
186 | 99 | ret = SupervisorServer().run() |
187 | 100 | print ret |
|
0 commit comments