|
12 | 12 | from subprocess import check_output |
13 | 13 | from time import time, sleep |
14 | 14 |
|
15 | | -class VarnishServer(): |
| 15 | +class VarnishServer(protobix.SampleProbe): |
| 16 | + |
| 17 | + __version__ = '0.0.9' |
16 | 18 |
|
17 | | - __version__ = '0.0.8' |
18 | | - ZBX_CONN_ERR = 'ERR - unable to send data to Zabbix [%s]' |
19 | 19 | METRICS = [ |
20 | 20 | ('cache[hit]', 'MAIN.cache_hit'), |
21 | 21 | ('cache[hitpass]', 'MAIN.cache_hitpass'), |
@@ -49,116 +49,45 @@ class VarnishServer(): |
49 | 49 | ('thread[queuelen]', 'MAIN.thread_queue_len') |
50 | 50 | ] |
51 | 51 |
|
52 | | - def _parse_args(self): |
53 | | - ''' Parse the script arguments |
54 | | - ''' |
55 | | - parser = optparse.OptionParser() |
56 | | - |
57 | | - parser.add_option("-d", "--dry", action="store_true", |
58 | | - help="Performs Varnish call but do not send " |
59 | | - "anything to the Zabbix server. Can be used " |
60 | | - "for both Update & Discovery mode") |
61 | | - parser.add_option("-D", "--debug", action="store_true", |
62 | | - help="Enable debug mode. This will prevent bulk send " |
63 | | - "operations and force sending items one after the " |
64 | | - "other, displaying result for each one") |
65 | | - parser.add_option("-v", "--verbose", action="store_true", |
66 | | - help="When used with debug option, will force value " |
67 | | - "display for each items managed. Beware that it " |
68 | | - "can be pretty too much verbose, specialy for LLD") |
| 52 | + def _get_varnishstat(self,hostname): |
| 53 | + varnish_stats = simplejson.loads( |
| 54 | + check_output( |
| 55 | + ['varnishstat', '-n', socket.gethostname(), '-1', '-j'] |
| 56 | + ) |
| 57 | + ) |
| 58 | + timestamp = int(time()) |
| 59 | + return varnish_stats, timestamp |
69 | 60 |
|
70 | | - mode_group = optparse.OptionGroup(parser, "Program Mode") |
71 | | - mode_group.add_option("--update-items", action="store_const", |
72 | | - dest="mode", const="update_items", |
73 | | - help="Get & send items to Zabbix. This is the default " |
74 | | - "behaviour even if option is not specified") |
75 | | - mode_group.add_option("--discovery", action="store_const", |
76 | | - dest="mode", const="discovery", |
77 | | - help="If specified, will perform Zabbix Low Level " |
78 | | - "Discovery on Varnish. " |
79 | | - "Default is to get & send items") |
80 | | - parser.add_option_group(mode_group) |
81 | | - parser.set_defaults(mode="update_items") |
| 61 | + def _parse_args(self): |
| 62 | + # Parse the script arguments |
| 63 | + # Common part |
| 64 | + parser = super( VarnishServer, self)._parse_args() |
82 | 65 |
|
| 66 | + # Varnish options |
83 | 67 | general_options = optparse.OptionGroup(parser, "Varnish Configuration") |
84 | | - general_options.add_option("-H", "--host", metavar="HOST", default="localhost", |
| 68 | + general_options.add_option("-H", "--host", default="localhost", |
85 | 69 | help="Varnish server hostname") |
86 | | - general_options.add_option("-p", "--port", help="Varnish server port", |
87 | | - default=6379) |
| 70 | + general_options.add_option("-P", "--port", default=6379, |
| 71 | + help="Varnish server port") |
88 | 72 | parser.add_option_group(general_options) |
89 | 73 |
|
90 | | - polling_options = optparse.OptionGroup(parser, "Zabbix configuration") |
91 | | - polling_options.add_option("--zabbix-server", metavar="HOST", |
92 | | - default="localhost", |
93 | | - help="The hostname of Zabbix server or " |
94 | | - "proxy, default is localhost.") |
95 | | - polling_options.add_option("--zabbix-port", metavar="PORT", default=10051, |
96 | | - help="The port on which the Zabbix server or " |
97 | | - "proxy is running, default is 10051.") |
98 | | - parser.add_option_group(polling_options) |
| 74 | + (options, args) = parser.parse_args() |
| 75 | + return (options, args) |
99 | 76 |
|
100 | | - return parser.parse_args() |
| 77 | + def _init_probe(self): |
| 78 | + if self.options.host == 'localhost': |
| 79 | + self.hostname = socket.getfqdn() |
| 80 | + else: |
| 81 | + self.hostname = self.options.host |
101 | 82 |
|
102 | 83 | def _get_metrics(self, hostname): |
103 | 84 | """ Get Varnish stats and parse it """ |
104 | 85 | data = {} |
105 | | - data[hostname] = {} |
106 | 86 | stats, timestamp = self._get_varnishstat(hostname) |
107 | 87 | for (key, metric) in self.METRICS: |
108 | | - data[hostname]["varnish.%s"%key] = stats[metric]['value'] |
109 | | - return data |
110 | | - |
111 | | - def _get_varnishstat(self,hostname): |
112 | | - varnish_stats = simplejson.loads(check_output(['varnishstat', '-n', socket.gethostname(), '-1', '-j'])) |
113 | | - timestamp = int(time()) |
114 | | - return varnish_stats, timestamp |
115 | | - |
116 | | - def _init_container(self): |
117 | | - zbx_container = protobix.DataContainer( |
118 | | - data_type = 'items', |
119 | | - zbx_host = self.options.zabbix_server, |
120 | | - zbx_port = int(self.options.zabbix_port), |
121 | | - debug = self.options.debug, |
122 | | - dryrun = self.options.dry |
123 | | - ) |
124 | | - zbx_container.data_type = 'items' |
125 | | - return zbx_container |
126 | | - |
127 | | - def run(self): |
128 | | - (self.options, args) = self._parse_args() |
129 | | - if self.options.host == 'localhost': |
130 | | - hostname = socket.getfqdn() |
131 | | - else: |
132 | | - hostname = self.options.host |
133 | | - |
134 | | - # Step 1: init container |
135 | | - try: |
136 | | - zbx_container = self._init_container() |
137 | | - except: |
138 | | - return 1 |
139 | | - |
140 | | - # Step 2: get data |
141 | | - try: |
142 | | - data = self._get_metrics(hostname) |
143 | | - except: |
144 | | - return 2 |
145 | | - |
146 | | - # Step 3: format & load data into container |
147 | | - try: |
148 | | - zbx_container.add(data) |
149 | | - zbx_container.add_item(hostname, "varnish.zbx_version", self.__version__) |
150 | | - except: |
151 | | - return 3 |
152 | | - |
153 | | - # Step 4: send container data to Zabbix server |
154 | | - try: |
155 | | - zbx_container.send(zbx_container) |
156 | | - except protobix.SenderException as zbx_e: |
157 | | - if self.options.debug: |
158 | | - print self.ZBX_CONN_ERR % zbx_e.err_text |
159 | | - return 4 |
160 | | - # Everything went fine. Let's return 0 and exit |
161 | | - return 0 |
| 88 | + data["varnish.%s" % key] = stats[metric]['value'] |
| 89 | + data['varnish.zbx_version'] = self.__version__ |
| 90 | + return { hostname: data } |
162 | 91 |
|
163 | 92 | if __name__ == '__main__': |
164 | 93 | ret = VarnishServer().run() |
|
0 commit comments