def __init__(self, port = 80, hostname = 'localhost', gmetric = None, config = None): if port is None: self.port = 80 else: self.port = port logFileName = 'gmetric-apache-' + hostname + '-' + str(self.port) + '.log' Gmetric.__init__(self, logFileName, gmetric, config) if hostname is not None: self.hostname = hostname
class emitter(object): """ A consumer of metrics that sends via gmetric """ def __init__(self, host, port, protocol): self.g = Gmetric(host, port, protocol) def addMetric(self, values, host=None): logging.debug("sending %s = %s", values['NAME'], values['VAL']) logging.debug("DICT = %s", str(values)) self.g.send(values['NAME'], values['VAL'], values['TYPE'], values['UNITS'], values['SLOPE'], values['TMAX'], values['DMAX'])
def __init__(self, mysql_username, mysql_password, mysql_hostname = 'localhost', mysql_port = None, gmetric = None, config = None): if mysql_port is None: self.mysqlPort = 3306 else: self.mysqlPort = mysql_port logFileName = 'gmetric-mysql-' + mysql_hostname + '-' + str(self.mysqlPort) + '.log' Gmetric.__init__(self, logFileName, gmetric, config) if mysql_hostname is not None: self.mysql_hostname = mysql_hostname self.connection = MySQLdb.connect (host = self.mysql_hostname, port = self.mysqlPort, user = mysql_username, passwd = mysql_password)
if len(splits) == 2: ganglia_port = splits[1] elif o == "--group": ganglia_metric_group_name = a # process arguments row_id = None table_name = None if len(args) > 1: table_name = args[0] row_id = args[1] hbc = None try: hbc = HBaseConnection(thrift_server, thrift_server_port) gmetric = Gmetric(ganglia_host, ganglia_port, ganglia_protocol) hbm = HBaseMetric(hbc, gmetric) gmc = GmetricConfig(type="int32", group_name=ganglia_metric_group_name) hbm.emit_row(table_name, row_id, gmc) except Thrift.TException, tx: logging.error("Thrift exception: %s" % (tx.message)) finally: if hbc: hbc.close() if __name__ == "__main__": console = logging.StreamHandler() console.setLevel(logging.DEBUG) logging.getLogger('').addHandler(console) sys.exit(main())
#!/usr/bin/env python import psyco psyco.full() from gmetric import Gmetric if __name__ == '__main__': g = Gmetric('localhost', 4001, 'udp') for i in xrange(100000): g.send('foo', 'bar')
def update_metric(self): tmp_ckie = 'auth_cookie='+self.cookie+';user=admin; Max-Age=3600; Path=/' hdr=dict() hdr['Cookie']=tmp_ckie hdr['Content-Type']='application/json' agg_rx = 0 agg_tx = 0 send_list = [] i = 0 for b in self.intf: url_intf = b.replace("/", "%2F") tmp_url='http://'+self.ip+':8090'+ '/nos/api/info/statistics/interface/' + url_intf print tmp_url ret = requests.get(tmp_url, headers=hdr, auth=('admin','admin'), verify=False, timeout=10) json_report = ret.json() m = { } infstr = json_report['if_name'] intfname = infstr.replace("/","_") m['name'] = self.ip + '_Rxrate_' + intfname if self.prev_rx_ctr[i] is 0: m['value'] = 0 else: m['value'] = (int) (((int) (json_report['rx_pkts']) - self.prev_rx_ctr[i]) / (self.refresh_rate)) self.prev_rx_ctr[i] = (int) (json_report['rx_pkts']) agg_rx = (int) (m['value']) + agg_rx m['time_max'] = 3 m['value_type'] = 'uint32' m['units'] = 'C' m['format'] = '%u', m['slope'] = 'both' m['description'] = 'rx rate' m['groups'] = 'rx_rate' send_list.append(m.copy()) m['name'] = self.ip + '_Txrate_' + intfname if self.prev_tx_ctr[i] is 0: m['value'] = 0 else: m['value'] = (int) (((int) (json_report['tx_pkts']) - self.prev_tx_ctr[i]) / (self.refresh_rate)) self.prev_tx_ctr[i] = (int) (json_report['tx_pkts']) agg_tx = (int) (m['value']) + agg_tx m['description'] = 'txrate' m['groups'] = 'tx_rate' send_list.append(m.copy()) i = i + 1 m['name'] = self.ip + '_Agg_Rxrate' m['value'] = agg_rx m['description'] = 'device_rx rate' m['groups'] = 'device_rx_rate' send_list.append(m.copy()) m['name'] = self.ip + '_Agg_Txrate' m['value'] = agg_tx m['description'] = 'device_tx rate' m['groups'] = 'device_tx_rate' send_list.append(m.copy()) payload = {} payload['req-id'] = 1 payload['request-type'] = 'port-drops' intf_list_dict = {} intf_list_dict['interface-list'] = self.intf payload['request-params'] = intf_list_dict tmp_url='http://'+self.ip+':8090'+ '/nos/api/info/telemetry/bst/congestion-drop-counters' ret = requests.post(tmp_url, headers=hdr, auth=('admin','admin'), json = payload, verify=False) json_report = ret.json() mylist = json_report['congestion-ctr'] i = 0 agg_cdrops = 0 for b in mylist: mystr = b['interface'] myname = mystr.replace("/","_") m['name'] = 'CDrp' + myname if self.prev_ctr[i] is 0: m['value'] = 0 else: m['value'] = (int) (((int) (b['ctr']) - self.prev_ctr[i]) / (self.refresh_rate)) agg_cdrops = m['value'] + agg_cdrops self.prev_ctr[i] = (int) (b['ctr']) m['description'] = 'Congestion drop rate' m['groups'] = 'cgsn_drop_rate' send_list.append(m.copy()) i = i + 1 m['name'] = self.ip + '_Agg_CgsnRate' m['value'] = agg_cdrops m['description'] = 'device_cgsn rate' m['groups'] = 'device_cgsn_rate' send_list.append(m.copy()) pull.logger.info("input json report %s" % json.dumps(json_report)) if send_list is None or len(send_list) == 0: pull.logger.debug("no metrics find") pull.logger.debug("%d metrics generated" % (len(send_list))) g = Gmetric('239.2.11.71', '8649', 'multicast') for m in send_list: s = self.ip fake_host = s.replace(".", "_") g.send(m['name'], m['value'], m['value_type'], m['units'], m['slope'],m['time_max'], 0, m['groups'], '%s:%s' %(self.ip, fake_host)) print("IP: %-15s updating metric %s %u (%s:%s)" % (self.ip, m['name'], int(m['value']), self.ip, fake_host)) '''
def init_thread(params): thread = UpdateMetricThread(params) thread.start() return thread def init_switch_monitors(): switch_monitors = [] for s in conf.switches_interface: switch_monitors.append((s, monitor_init({"sw_intf" : s}))) return switch_monitors if __name__ == '__main__': pull = LogHandler("telemetry.log", 1024*1024*50, 5, "INFO") switch_monitors = init_switch_monitors() g = Gmetric('239.2.11.71', '8649', 'multicast') try: while True: for s in switch_monitors: print "============================================================================" print "updating metrics of switch %s" % s[0] # sleep 5 seconds before the next query time.sleep(5) except KeyboardInterrupt: time.sleep(0.2) os._exit(1) except StandardError: traceback.print_exc()
gmond_host = conf.get("metricd", "gmond_host") gmond_port = conf.get("metricd", "gmond_port") gmond_protocol = conf.get("metricd", "gmond_protocol") counter_map = {} def rrd_name(name, direction): return "wissbi.%s.%s" % (name.replace("/", "__").replace(":", "."), direction) def load_counter(name): try: counter_map[name] = int(rrdtool.info("%s/%s.rrd" % (rrd_folder, name))["ds[sum].last_ds"]) except: counter_map[name] = 0 metric_sender = Gmetric(gmond_host, gmond_port, gmond_protocol) while True: line = os.sys.stdin.readline().strip() if len(line) == 0: break (name, direction, value) = line.split(",") name = rrd_name(name, direction) if not name in counter_map: load_counter(name) new_counter = counter_map[name] + int(value) if new_counter > 0xffffffff: new_counter = new_counter - 0xffffffff - 1 metric_sender.send(name, new_counter, TYPE="uint32", SLOPE="positive", GROUP="wissbi")
def update_metric(self): tmp_ckie = 'auth_cookie=' + self.cookie + ';user=admin; Max-Age=3600; Path=/' hdr = dict() hdr['Cookie'] = tmp_ckie hdr['Content-Type'] = 'application/json' tmp_url = 'http://' + self.ip + ':8090' + '/nos/api/info/telemetry/bst/report' payload = {} payload['include-ingress-port-priority-group'] = 1 payload['include-ingress-port-service-pool'] = 1 payload['include-ingress-service-pool'] = 1 payload['include-egress-port-service-pool'] = 1 payload['include-egress-service-pool'] = 1 payload['include-device'] = 1 ret = requests.post(tmp_url, headers=hdr, auth=('admin', 'admin'), json=payload, verify=False) json_report = ret.json() m = {} m['time_max'] = 3 m['value_type'] = 'uint32' m['units'] = 'C' m['format'] = '%u', m['slope'] = 'both' send_list = [] realm_list = json_report['report'] for i in range(len(realm_list)): realm = realm_list[i] if not realm.has_key('realm'): break if realm['realm'] == 'device': print "device realm" m['value'] = (int)(realm['data']) m['description'] = 'Device Realm' m['groups'] = 'Device Realm' m['name'] = '_Device' print m send_list.append(m.copy()) if realm['realm'] == 'ingress-port-priority-group': data_list = realm['data'] val = 0 for j in range(len(data_list)): data = data_list[j] arr = data['data'] pg = arr[0][0] val = val + (int)(arr[0][1]) m['value'] = (int)(val / len(data_list)) m['description'] = 'Ingress port priority group' m['groups'] = 'Ingress Port Priority Group' m['name'] = '_aggregate_ingress_port_priority_group_' + str(pg) print m send_list.append(m.copy()) elif realm['realm'] == 'ingress-port-service-pool': data_list = realm['data'] val = 0 for j in range(len(data_list)): data = data_list[j] arr = data['data'] service_pool = arr[0][0] val = val + (int)(arr[0][1]) m['value'] = (int)(val / len(data_list)) m['description'] = 'Ingress port service pool' m['groups'] = 'Ingress Port Service Pool' m['name'] = '_aggregate_ingress-port-service_pool_' + str( service_pool) send_list.append(m.copy()) elif realm['realm'] == 'ingress-service-pool': data_list = realm['data'] service_pool = data_list[0][0] m['value'] = (int)(data_list[0][1]) m['description'] = 'Ingress service pool' m['groups'] = 'Ingress Service Pool' m['name'] = '_ingress-service-pool_' + str(service_pool) send_list.append(m.copy()) elif realm['realm'] == 'egress-port-service-pool': data_list = realm['data'] val = 0 for j in range(len(data_list)): data = data_list[j] arr = data['data'] service_pool = arr[0][0] val = val + (int)(arr[0][1]) m['value'] = (int)(val / len(data_list)) m['description'] = 'Egress port service pool' m['groups'] = 'Egress Port Service Pool' m['name'] = '_aggregate_egress-port-service-pool_' + str( service_pool) send_list.append(m.copy()) elif realm['realm'] == 'egress-service-pool': data_list = realm['data'] service_pool = data_list[0][0] m['value'] = (int)(data_list[0][1]) m['description'] = 'Egress service pool' m['groups'] = 'Egress Service Pool' m['name'] = '_egress-service-pool_' + str(service_pool) send_list.append(m.copy()) #pdb.set_trace() pull.logger.debug("input json report %s" % json.dumps(json_report)) if send_list is None or len(send_list) == 0: pull.logger.debug("no metrics find") return pull.logger.debug("%d metrics generated" % (len(send_list))) g = Gmetric('239.2.11.71', '8649', 'multicast') for m in send_list: s = self.ip fake_host = s.replace(".", "_") m['name'] = self.ip + m['name'] g.send(m['name'], m['value'], m['value_type'], m['units'], m['slope'], m['time_max'], 0, m['groups'], '%s:%s' % (self.ip, fake_host)) pull.logger.debug( "Switch IP: %-15s updating metric %s, metric value: %d (%s:%s)" % (self.ip, m['name'], int(m['value']), self.ip, fake_host)) print( "Switch IP: %-15s updating metric %s, metric value: %d (%s:%s)" % (self.ip, m['name'], int(m['value']), self.ip, fake_host))
def __init__(self, host, port, protocol): self.g = Gmetric(host, port, protocol)