def init(self): logger.info("[riemann broker] I init the %s server connection to %s:%d" % (self.get_name(), str(self.host), self.port)) if self.use_udp: transport = UDPTransport(self.host, self.port) else: transport = TCPTransport(self.host, self.port) transport.connect() self.client = Client(transport)
def test_not_conected_post(self): """ Make a call to a dummy resource and check if monitor calls have been called. """ with patch.object( self.api, "_get_riemann_client", lambda: QueuedClient(UDPTransport("10.10.10.90"))): _wsgi_read(self.simulate_request("/", method="POST")) self.assertTrue(self._wait_for_clear()) # wait for worker self.assertEqual(self.srmock.status[:3], "405")
def __init__(self, config=None): # Initialize Handler Handler.__init__(self, config) if riemann_client is None: logging.error("Failed to load riemann_client module") return # Initialize options self.host = self.config['host'] self.port = int(self.config['port']) self.transport = self.config['transport'] # Initialize client if self.transport == 'tcp': self.transport = TCPTransport(self.host, self.port) else: self.transport = UDPTransport(self.host, self.port) self.client = Client(self.transport) self._connect()
def main(): args = parsed_args(sys.argv[1:]) logging_level = logging.DEBUG if args.debug else logging.WARNING logging.basicConfig(level=logging_level) try: if args.protocol == 'tcp': transport = TCPTransport(args.host, args.port, args.timeout) elif args.protocol == 'tls': transport = TLSTransport( args.host, args.port, args.timeout, keyfile=args.keyfile, certfile=args.certfile, ca_certs=args.ca_certs) elif args.protocol == 'udp': transport = UDPTransport(args.host, args.port) else: raise RuntimeError('Transport {} is not supported'.format(args.transport)) fp = fping.Fping(args.fping_cmd, args.probe, interval=args.interval) client = Client(transport) for summary in fp.ping_summaries(args.target): try: send_summary(client, summary) except SendError as e: logging.warning('Unable to send {} to {} ({})'.format( e.summary, args.host, e)) except Exception as e: if args.debug: raise else: logging.error('Unhandled exception ({})'.format(e)) return 1 return 0
class RiemannHandler(Handler): def __init__(self, config=None): # Initialize Handler Handler.__init__(self, config) if riemann_client is None: logging.error("Failed to load riemann_client module") return # Initialize options self.host = self.config['host'] self.port = int(self.config['port']) self.transport = self.config['transport'] # Initialize client if self.transport == 'tcp': self.transport = TCPTransport(self.host, self.port) else: self.transport = UDPTransport(self.host, self.port) self.client = Client(self.transport) self._connect() def get_default_config_help(self): """ Returns the help text for the configuration options for this handler """ config = super(RiemannHandler, self).get_default_config_help() config.update({ 'host': '', 'port': '', 'transport': 'tcp or udp', }) return config def get_default_config(self): """ Return the default config for the handler """ config = super(RiemannHandler, self).get_default_config() config.update({ 'host': '', 'port': 123, 'transport': 'tcp', }) return config def process(self, metric): """ Send a metric to Riemann. """ event = self._metric_to_riemann_event(metric) try: self.client.send_event(event) except Exception as e: self.log.error( "RiemannHandler: Error sending event to Riemann: %s", e) def _metric_to_riemann_event(self, metric): """ Convert a metric to a dictionary representing a Riemann event. """ # Riemann has a separate "host" field, so remove from the path. path = '%s.%s.%s' % ( metric.getPathPrefix(), metric.getCollectorPath(), metric.getMetricPath() ) return self.client.create_event({ 'host': metric.host, 'service': path, 'time': metric.timestamp, 'metric_f': float(metric.value), 'ttl': metric.ttl, }) def _connect(self): self.transport.connect() def _close(self): """ Disconnect from Riemann. """ if hasattr(self, 'transport'): self.transport.disconnect() def __del__(self): self._close()