33results that can then be used by Zabbix.
44https://github.com/jasonmcintosh/rabbitmq-zabbix
55'''
6+ from __future__ import unicode_literals
7+
8+ import io
69import json
710import optparse
811import socket
912import urllib2
1013import subprocess
11- import tempfile
1214import os
1315import logging
1416
@@ -104,7 +106,7 @@ def check_queue(self, filters=None):
104106 if not filters :
105107 filters = [{}]
106108
107- rdatafile = tempfile . NamedTemporaryFile ( delete = False )
109+ buffer = io . StringIO ( )
108110
109111 try :
110112 for queue in self .call_api ('queues' ):
@@ -117,18 +119,16 @@ def check_queue(self, filters=None):
117119 success = True
118120 break
119121 if success :
120- self ._prepare_data (queue , rdatafile )
122+ self ._prepare_data (queue , buffer )
121123 except urllib2 .HTTPError as err :
122124 if err .code == 404 :
123- rdatafile .close ()
124- os .unlink (rdatafile .name )
125+ buffer .close ()
125126 return return_code
126127 else :
127128 raise err
128129
129- rdatafile .close ()
130- return_code = self ._send_data (rdatafile )
131- os .unlink (rdatafile .name )
130+ return_code = self ._send_data (buffer )
131+ buffer .close ()
132132 return return_code
133133
134134 def check_shovel (self , filters = None ):
@@ -137,7 +137,7 @@ def check_shovel(self, filters=None):
137137 if not filters :
138138 filters = [{}]
139139
140- rdatafile = tempfile . NamedTemporaryFile ( delete = False )
140+ buffer = io . StringIO ( )
141141
142142 try :
143143 for shovel in self .call_api ('shovels' ):
@@ -154,48 +154,47 @@ def check_shovel(self, filters=None):
154154 key = key .format (shovel ['vhost' ], 'state' , shovel ['name' ])
155155 value = shovel .get ('state' , 0 )
156156 logging .debug ("SENDER_DATA: - %s %s" % (key ,value ))
157- rdatafile .write ("- %s %s\n " % (key , value ))
157+ buffer .write ("- %s %s\n " % (key , value ))
158158 except urllib2 .HTTPError as err :
159159 if err .code == 404 :
160- rdatafile .close ()
161- os .unlink (rdatafile .name )
160+ buffer .close ()
162161 return return_code
163162 else :
164163 raise err
165164
166- rdatafile .close ()
167- return_code = self ._send_data (rdatafile )
168- os .unlink (rdatafile .name )
165+ return_code = self ._send_data (buffer )
166+ buffer .close ()
169167 return return_code
170168
171- def _prepare_data (self , queue , tmpfile ):
169+ def _prepare_data (self , queue , file ):
172170 '''Prepare the queue data for sending'''
173171 for item in ['memory' , 'messages' , 'messages_unacknowledged' ,
174172 'consumers' ]:
175173 key = '"rabbitmq.queues[{0},queue_{1},{2}]"'
176174 key = key .format (queue ['vhost' ], item , queue ['name' ])
177175 value = queue .get (item , 0 )
178176 logging .debug ("SENDER_DATA: - %s %s" % (key ,value ))
179- tmpfile .write ("- %s %s\n " % (key , value ))
177+ file .write ("- %s %s\n " % (key , value ))
180178 ## This is a non standard bit of information added after the standard items
181179 for item in ['deliver_get' , 'publish' , 'ack' ]:
182180 key = '"rabbitmq.queues[{0},queue_message_stats_{1},{2}]"'
183181 key = key .format (queue ['vhost' ], item , queue ['name' ])
184182 value = queue .get ('message_stats' , {}).get (item , 0 )
185183 logging .debug ("SENDER_DATA: - %s %s" % (key ,value ))
186- tmpfile .write ("- %s %s\n " % (key , value ))
187-
184+ file .write ("- %s %s\n " % (key , value ))
188185
189- def _send_data (self , tmpfile ):
186+ def _send_data (self , file ):
190187 '''Send the queue data to Zabbix.'''
191- args = 'zabbix_sender -vv -c {0} -i {1} '
188+ args = 'zabbix_sender -vv -c {0} -i - '
192189 if self .senderhostname :
193190 args = args + " -s " + self .senderhostname
194191 return_code = 0
195- process = subprocess .Popen (args .format (self .conf , tmpfile .name ),
196- shell = True , stdout = subprocess .PIPE ,
192+ process = subprocess .Popen (args .format (self .conf ),
193+ shell = True ,
194+ stdin = subprocess .PIPE ,
195+ stdout = subprocess .PIPE ,
197196 stderr = subprocess .PIPE )
198- out , err = process .communicate ()
197+ out , err = process .communicate (input = file . getvalue () )
199198 logging .debug ("Finished sending data" )
200199 return_code = process .wait ()
201200 logging .info ("Found return code of " + str (return_code ))
@@ -264,7 +263,7 @@ def main():
264263 logging .debug ("Started trying to process data" )
265264 api = RabbitMQAPI (user_name = options .username , password = options .password ,
266265 host_name = options .hostname , port = options .port ,
267- conf = options .conf , senderhostname = options .senderhostname ,
266+ conf = options .conf , senderhostname = options .senderhostname ,
268267 protocol = options .protocol )
269268 if options .filters :
270269 try :
0 commit comments