Skip to content

Commit 0de1292

Browse files
author
Jean Baptiste Favre
committed
Update varnish4 plugin for SampleProbe
1 parent 78aae0a commit 0de1292

File tree

1 file changed

+29
-100
lines changed

1 file changed

+29
-100
lines changed

scripts/varnish4.py

Lines changed: 29 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from subprocess import check_output
1313
from time import time, sleep
1414

15-
class VarnishServer():
15+
class VarnishServer(protobix.SampleProbe):
16+
17+
__version__ = '0.0.9'
1618

17-
__version__ = '0.0.8'
18-
ZBX_CONN_ERR = 'ERR - unable to send data to Zabbix [%s]'
1919
METRICS = [
2020
('cache[hit]', 'MAIN.cache_hit'),
2121
('cache[hitpass]', 'MAIN.cache_hitpass'),
@@ -49,116 +49,45 @@ class VarnishServer():
4949
('thread[queuelen]', 'MAIN.thread_queue_len')
5050
]
5151

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
6960

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()
8265

66+
# Varnish options
8367
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",
8569
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")
8872
parser.add_option_group(general_options)
8973

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)
9976

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
10182

10283
def _get_metrics(self, hostname):
10384
""" Get Varnish stats and parse it """
10485
data = {}
105-
data[hostname] = {}
10686
stats, timestamp = self._get_varnishstat(hostname)
10787
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 }
16291

16392
if __name__ == '__main__':
16493
ret = VarnishServer().run()

0 commit comments

Comments
 (0)