def main(): rabbit = None try: # Ensure there are no paralell runs of this script lock.acquire(timeout=5) # Connect to Rabbit nullwrite = NullWriter() oldstdout = sys.stdout sys.stdout = nullwrite # disable output rabbit = RabbitClient('%s:%s' % (rabbitmq['host'], rabbitmq['port']), rabbitmq['username'], rabbitmq['password']) if not rabbit.is_alive(): raise Exception('Cannot connect to RabbitMQ') vhost = rabbit.get_vhost(rabbitmq['vhost']) queues = rabbit.get_queues(rabbitmq['vhost']) sys.stdout = oldstdout # enable output # Build outcome metrics = [] for key in keys: if type(key) == dict and 'vhost' in key: for subkey in key['vhost']: if subkey in vhost: metrics.append( Metric(rabbitmq['host'], 'rabbitmq.%s.%s' % ('vhost', subkey), vhost[subkey])) elif type(key) == dict and 'vhost.message_stats' in key: for subkey in key['vhost.message_stats']: if subkey in vhost['message_stats']: metrics.append( Metric( rabbitmq['host'], 'rabbitmq.%s.%s' % ('vhost.message_stats', subkey), vhost['message_stats'][subkey])) elif type(key) == dict and 'queues' in key: for queue in queues: for subkey in key['queues']: if subkey in queue: metrics.append( Metric( rabbitmq['host'], 'rabbitmq.%s.%s[%s]' % ('queue', subkey, queue['name']), queue[subkey])) # Send packet to zabbix send_to_zabbix(metrics, zabbix_host, zabbix_port) except LockTimeout: print 'Lock not acquired, exiting' except AlreadyLocked: print 'Already locked, exiting' except Exception, e: print type(e) print 'Error: %s' % e
def main(): rabbit = None try: # Ensure there are no paralell runs of this script lock.acquire(timeout=5) # Connect to Rabbit nullwrite = NullWriter() oldstdout = sys.stdout sys.stdout = nullwrite # disable output rabbit = RabbitClient('%s:%s' % (rabbitmq['host'], rabbitmq['port']), rabbitmq['username'], rabbitmq['password']) if not rabbit.is_alive(): raise Exception('Cannot connect to RabbitMQ') vhost = rabbit.get_vhost(rabbitmq['vhost']) queues = rabbit.get_queues(rabbitmq['vhost']) sys.stdout = oldstdout # enable output # Build outcome metrics = [] for key in keys: if type(key) == dict and 'vhost' in key: for subkey in key['vhost']: if subkey in vhost: metrics.append(Metric(rabbitmq['host'], 'rabbitmq.%s.%s' % ('vhost', subkey), vhost[subkey])) elif type(key) == dict and 'vhost.message_stats' in key: for subkey in key['vhost.message_stats']: if subkey in vhost['message_stats']: metrics.append(Metric(rabbitmq['host'], 'rabbitmq.%s.%s' % ('vhost.message_stats', subkey), vhost['message_stats'][subkey])) elif type(key) == dict and 'queues' in key: for queue in queues: for subkey in key['queues']: if subkey in queue: metrics.append(Metric(rabbitmq['host'], 'rabbitmq.%s.%s[%s]' % ('queue', subkey, queue['name']), queue[subkey])) # Send packet to zabbix send_to_zabbix(metrics, zabbix_host, zabbix_port) except LockTimeout: print 'Lock not acquired, exiting' except AlreadyLocked: print 'Already locked, exiting' except Exception, e: print type(e) print 'Error: %s' % e