def dashboard(): c = bernhard.Client() q = c.query("true") events = {} services = [] for e in q: if e.event.host in events: events[e.event.host].update({ e.event.service: e.event.metric_d or e.event.metric_f or e.event.metric_sint64 }) else: events[e.event.host] = { e.event.service: e.event.metric_d or e.event.metric_f or e.event.metric_sint64 } if e.event.service in services: pass else: services.append(e.event.service) return flask.render_template("dashboard.html", riemann=events, services=services, config={ "riemann_host": "localhost", "riemann_port": 3306 })
def main(): client = bernhard.Client(host='127.0.0.1', port=5555) event_service = get_service_name() event_value = get_event_value() event_hostname = get_hostname() if event_value > 85: event_state = 'critical' elif event_value >= 75: event_state = 'warning' else: event_state = 'ok' print('host: {}, service: {}, metric: {}'.format(event_hostname, event_service, str(event_value))) client.send({ 'host': event_hostname, 'service': event_service, 'description': 'blah', 'state': event_state, 'ttl': 25, 'metric': event_value })
def __init__(self): if not hasattr(microweb.settings, 'RIEMANN_ENABLED'): raise AssertionError, 'Please declare RIEMANN_ENABLED in settings.py' if microweb.settings.RIEMANN_ENABLED: self.client = bernhard.Client()
def __init__(self): if not hasattr(settings, 'RIEMANN_ENABLED'): raise AssertionError, 'Please declare RIEMANN_ENABLED in settings.py' if settings.RIEMANN_ENABLED: self.client = bernhard.Client(host=settings.RIEMANN_HOST, transport=bernhard.UDPTransport)
def main(): parser = OptionParser() set_options(parser) (options, args) = parser.parse_args() e = vars(options) print e riemann = bernhard.Client() riemann.send(e) print riemann.query('true')
def psql(riemann_host, riemann_port, sqlquery): """monitor query from a postgresql database""" logging.basicConfig(level=logging.INFO) logger.info("version %s starting", util.get_version()) util.watch_report_loop( lambda: bernhard.Client(riemann_host, riemann_port), functools.partial(watch_psql, sqlquery), 10, )
def init(self): """Init the connection to the Riemann server.""" if not self.export_enable: return None try: client = bernhard.Client(host=self.host, port=self.port) return client except Exception as e: logger.critical("Connection to Riemann failed : %s " % e) return None
def relay(riemann_host, riemann_port, url): """monitor relay server""" logging.basicConfig(level=logging.INFO) logger.info("version %s starting", util.get_version()) logger.info("watching %s", url) util.watch_report_loop( lambda: bernhard.Client(riemann_host, riemann_port), functools.partial(watch_relay, url), 10, )
def etherscan(riemann_host, riemann_port, chain): """monitor etherscan for latest blocknumber""" logging.basicConfig(level=logging.INFO) logger.info("version %s starting", util.get_version()) logger.info("watching %s at %s", chain, name2url[chain]) util.watch_report_loop( lambda: bernhard.Client(riemann_host, riemann_port), functools.partial(watch_etherscan, chain), 10, )
def auction_backend(riemann_host, riemann_port, base_url): """Monitor auction backend for availability.""" logging.basicConfig(level=logging.INFO) logger.info(f"Version {util.get_version()} starting") logger.info(f"Watching '{base_url}' backend for availability") util.watch_report_loop( lambda: bernhard.Client(riemann_host, riemann_port), functools.partial(watch_auction_backend, base_url), 10, )
def website(riemann_host, riemann_port, url, original_hash): """Monitor website for changed sources.""" logging.basicConfig(level=logging.INFO) logger.info(f"version {util.get_version()} starting") logger.info(f"watching {url} sources with original hash {original_hash}") util.watch_report_loop( lambda: bernhard.Client(riemann_host, riemann_port), functools.partial(watch_website, url, original_hash), 120, )
def worker(n): cli = bernhard.Client(**RIEMANN_ARGS) while True: try: target, metric = RIEMANN_QUEUE.get() target = target.partition('?')[0] target = target.replace('/', '.') event = {'service': target, 'metric': metric} event.update(RIEMANN_SOURCE) result = cli.send(event=event) log.debug("Sent %s result %s", target, result) except Exception as e: log.error(e)
def __init__(self, config=None): # Initialize Handler Handler.__init__(self, config) # Initialize options self.host = self.config['host'] self.port = int(self.config['port']) self.transport = self.config.get('transport', 'tcp') # Initialize client if self.transport == 'tcp': transportCls = bernhard.TCPTransport else: transportCls = bernhard.UDPTransport self.client = bernhard.Client(self.host, self.port, transportCls)
def get_riemann(riemann_arg, default_ttl, log_param): import bernhard client = None if riemann_arg: global log log = log_param protocol, host, port = riemann_arg.split(':') port = int(port) if protocol == 'udp': proto_impl = bernhard.UDPTransport elif protocol == 'tcp': proto_impl = bernhard.TCPTransport else: log.error("protocol [%s] is not valid. Should be udp or tcp" % protocol) client = bernhard.Client(host, port, transport=proto_impl) return RiemannReporter(client, default_ttl)
def __init__(self, host, port): self.currentHost = socket.gethostname() self.buffer = "" self.state = None self.enabled = False self.attributes = {} self.begin = time.time() if not host: return try: import bernhard self.client = bernhard.Client(host=host, port=port) self.client.send({'host': self.currentHost, 'state': 'ok', 'service': "alibuild started"}) self.enabled = True info("Sending log data to %s:%s" % (host, port)) except Exception as e: info("RIEMANN_HOST %s:%s specified, however there was a problem initialising:" % (host, port)) info(e)
def __init__(self, config=None): # Initialize Handler Handler.__init__(self, config) if bernhard is None: logging.error("Failed to load bernhard 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': transportCls = bernhard.TCPTransport else: transportCls = bernhard.UDPTransport self.client = bernhard.Client(self.host, self.port, transportCls)
def jsonrpc(riemann_host, riemann_port, url, event_host_dwim): """watch geth/parity for latest block number""" logging.basicConfig(level=logging.INFO) logger.info("version %s starting", util.get_version()) logger.info("watching %s", url) if event_host_dwim: hostname = urlparse(url).hostname if hostname in ("localhost", "127.0.0.1"): event_host = socket.gethostname() else: event_host = hostname else: # Let's stay compatible with what we had before event_host = url util.watch_report_loop( lambda: bernhard.Client(riemann_host, riemann_port), functools.partial(watch_jsonrpc, url, event_host), 10, )
def set_property(node_id, property_name, value): """ Sends a riemann event which causes the state cache to set the node's property value """ logger.info("Setting {0} {1} property to '{2}'".format( node_id, property_name, value)) riemann_client = bernhard.Client(host="localhost") event = { "host": node_id, "service": property_name, "state": value, "tags": ["cosmo"], "description": json.dumps({ "node_id": node_id, "policy": "", "message": "" }) } riemann_client.send(event)
def report_main(): defaults = { "riemann_host": "localhost", "riemann_port": "5555", "interval": "60", "ttl": "120", "tag": "zyxel-gs1200", } config = configparser.ConfigParser(defaults=defaults) config.read(sys.argv[1:]) hosts = list(hosts_of_config(config)) interval = int(config["DEFAULT"]["interval"]) riemann_host = config["DEFAULT"]["riemann_host"] riemann_port = int(config["DEFAULT"]["riemann_port"]) tag = config["DEFAULT"]["tag"] ttl = int(config["DEFAULT"]["ttl"]) riemann_client = bernhard.Client(host=riemann_host, port=riemann_port) def it(): for h in hosts: for m in metrics_of_host(h): m["ttl"] = ttl m["tags"] = [tag] if not "state" in m: m["state"] = "ok" riemann_client.send(m) # TODO(mastensg): stop drift: https://github.com/blakev/gevent-tasks schedule.every(interval).seconds.do(it) schedule.run_all() while True: schedule.run_pending() time.sleep(schedule.idle_seconds())
def riemann_wrapper(client=bernhard.Client(), prefix="python", sep=".", host=None, exception_state='warning', send_exceptions=True, global_tags=['python'], global_attributes={}, logger=None): """Yield a riemann wrapper with default values for the bernhard client, host and prefix""" global_client = client global_host = host global_logger = logger def wrap_riemann(metric, client=global_client, host=global_host, tags=[], attributes={}, logger=global_logger): tags = global_tags + tags attributes = dict(list(global_attributes.items()) + list(attributes.items())) def send(event): if client: try: client.send(event) except bernhard.TransportError: log = _call_if_callable(logger) if log: log.exception('Failed to send Riemann event.') def riemann_decorator(f): @wraps(f) def decorated_function(*args, **kwargs): if host: hostname = host else: hostname = socket.gethostname() started = time.time() metric_name = prefix + sep + metric # Prefix takes precedence all_attributes = dict(list(attributes.items()) + [('prefix', prefix)]) try: response = f(*args, **kwargs) except Exception as e: if _call_if_callable(send_exceptions, e): send({'host': hostname, 'service': metric_name + "-exceptions", 'description': str(e), 'tags': tags + ['exception'], 'attributes': all_attributes, 'state': exception_state, 'metric': 1}) raise duration = (time.time() - started) * 1000 send({'host': hostname, 'service': metric_name + "-time", 'attributes': all_attributes, 'tags': tags + ['duration'], 'metric': duration}) return response return decorated_function return riemann_decorator return wrap_riemann
#! /usr/bin/env python import bernhard c = bernhard.Client() c.send({'host': 'myhost.foobar.com', 'service': 'myservice', 'metric': 12})
#!/usr/bin/python ################################################## # this script is launched by riemann.sh # # dependencies: riemann.sh, riemann-get-stats.sh # ################################################## import bernhard import subprocess c = bernhard.Client(host = 'monitor1') _thisServer = subprocess.check_output(['uname', '-n']).replace('\n','') #linux machine stats #subprocess.call('/etc/quill/riemann-get-stats.sh >/tmp/riemann-stats', shell=True) #now runs in riemann.sh lines = [line.rstrip() for line in open('/tmp/riemann-stats')] for item in lines: _key, _trash, _value = item.partition(' ') _value = _value.replace('%', '') _key = _key.replace('-', ' ') #cater fot ifstat's variable metric output if _value.endswith('K'): _value = float(_value[:-1]) / 1024 else: if _value.endswith('M'): _value = float(_value[:-1]) / 1024 / 1024 _state = 'ok' if "%" in _key: if float(_value) > 90: _state = 'critical'
import signal import sys import uuid # Catching gracefully ctrl+c def signal_handler(signal, frame): print "System down" sys.exit(0) signal.signal(signal.SIGINT, signal_handler) # Set hostname. my_hostname = '{{ ansible_fqdn }}' # Creating a default event. event = { 'host': str(my_hostname), 'service': "Heartbeat", } # Starting a Riemann client. client = bernhard.Client(host="{{ riemann_host }}") while True: # print "Sending Heartbeat " client.send(event) time.sleep({{interval}}) # print "System down"
def get_data(self, target): c = bernhard.Client(host=target['protocol'] + '://' + target['host'], port=target['port']) return c.query('host = "peewee-ThinkPad"')
stderr = proc.stderr.read() description = """ STDOUT >>> %s <<< STDERR >>> %s <<< """ % (stdout, stderr) if options.tcp: transport = bernhard.TCPTransport else: transport = bernhard.UDPTransport riemann = bernhard.Client(host=options.riemann_host, port=options.riemann_port, transport=transport) riemann_event = {} riemann_event["service"] = service riemann_event["description"] = description if options.state_from_stdout: riemann_event["state"] = stdout.strip() else: riemann_event["state"] = run_state(proc, state_table) if options.ttl: riemann_event["ttl"] = int(options.ttl) if options.host: riemann_event["host"] = options.host
class Feeder: def __init__(self, reader, logger): self.reader = reader self.logger = logger self.reset_clearance() def reset_clearance(self): self.next_clearance = datetime.now() + timedelta(seconds=1) def loop(self): for packet in self.reader: if datetime.now() > self.next_clearance: self.reset_clearance() self.logger.flush() self.logger.process_packet(packet) self.logger.flush() if __name__ == "__main__": if len(sys.argv) != 2: usage() # in our use case this is often a FIFO host = os.environ['RIEMANN_HOST'] pcapdata = PcapReader(sys.argv[1]) riemann = bernhard.Client(host=host, transport=bernhard.UDPTransport) packetlog = PacketLog(riemann) feeder = Feeder(pcapdata, packetlog) feeder.loop()
#!/usr/bin/env python import bernhard import re from subprocess import Popen, PIPE from socket import gethostname from sys import argv HOST = gethostname() SERVER = argv[1] TTL = int(argv[2]) c = bernhard.Client(SERVER) cciss = ['/usr/bin/cciss_vol_status', '/dev/cciss/c0d0'] output = Popen(cciss, stdout=PIPE, shell=False).stdout for line in output: event = {} event['host'] = HOST event['ttl'] = TTL event['tags'] = ['raid'] if re.match('.+:\s+ok.*$', line, flags=re.IGNORECASE): event['state'] = 'ok' else: event['state'] = 'critical' event['service'] = re.sub('\s+\(.+?\)|\s+status:.*|/dev/cciss/', '', line.strip())
def __init__(self): if settings.RIEMANN_ENABLED: self.client = bernhard.Client(host=settings.RIEMANN_HOST, transport=bernhard.UDPTransport) else: raise exceptions.MiddlewareNotUsed
def __init__(self, rm_tcp_host, rm_tcp_port, this_host): logger.debug("Use remote riemman uri: {}:{}".format( rm_tcp_host, rm_tcp_port)) self.client = bernhard.Client(host=rm_tcp_host, port=rm_tcp_port) logger.debug("Tag this worker by host={}".format(this_host)) self.this_host = this_host
import schedule import requests import bernhard import time from configparser import ConfigParser file = '/opt/nginx_plus_metrics_fetcher/code/metrics_config.ini' config = ConfigParser() config.read(file) ip = config['api_config']['ip'] port = config['api_config']['port'] api_version = config['api_config']['api_version'] riemann_host = config['riemann']['host'] riemann_port = config['riemann']['port'] url = "http://" + ip + ":" + port + "/api/" + api_version c = bernhard.Client(host=riemann_host, port=riemann_port) def connections(): response = requests.get(url + "/connections") json_response = response.json() c.send({'service': 'connections', 'attributes': json_response}) def ssl(): response = requests.get(url + "/ssl") json_response = response.json() c.send({'service': 'ssl', 'attributes': json_response})