Example #1
0
 def send(self, host, name, value, mtime):
     if self.tcollector_host_tag and host:
         stat = names.statname(None, name)
         mesg = "%s %s %s %s=%s" % (stat, mtime, value,
                                    cfg.tcollector_host_tag, host)
     else:
         stat = names.statname(host, name)
         mesg = "%s %s %s" % (stat, mtime, value)
     print mesg
Example #2
0
 def write(self, host, name, value, tstamp):
     target = statname(host, name)
     dtime = datetime.datetime.fromtimestamp(tstamp)
     time_lbl = dtime.strftime('%y%m%d %H:%M:%S')
     self.stdout.write('%s (%.1fs) %s %r\n' %
                       (time_lbl, tstamp - time.time(), target, value))
     self.stdout.flush()
 def write(self, host, name, value, tstamp):
     target = statname(host, name)
     dtime = datetime.datetime.fromtimestamp(tstamp)
     time_lbl = dtime.strftime('%y%m%d %H:%M:%S')
     self.stdout.write('%s (%.1fs) %s %r\n' % (time_lbl,
                                               tstamp - time.time(),
                                               target, value))
     self.stdout.flush()
Example #4
0
 def send(self, host, name, value, mtime):
     if self.collector_host_tag and host:
         stat = names.statname(None, name)
         tags = '%s=%s %s' % (self.opentsdb_host_tag, host, self.tags)
     else:
         stat = names.statname(host, name)
         tags = self.tags
     mesg = "put %s %s %s %s\n" % (stat, mtime, value, tags)
     for i in xrange(self.max_reconnects):
         try:
             self.sock.sendall(mesg)
             return
         except socket.error as err:
             log.error("Failed to send data to OpenTSDB server: %s", err)
             try:
                 self.reconnect()
             except socket.error as err:
                 log.error("Failed reconnect to OpenTSDB server: %s", err)
     log.error("Dropping message %s", mesg)
Example #5
0
 def send(self, host, name, value, mtime):
     if self.collector_host_tag and host:
         stat = names.statname(None, name)
         tags = '%s=%s %s' % (self.opentsdb_host_tag, host, self.tags)
     else:
         stat = names.statname(host, name)
         tags = self.tags
     mesg = "put %s %s %s %s\n" % (stat, mtime, value, tags)
     for i in xrange(self.max_reconnects):
         try:
             self.sock.sendall(mesg)
             return
         except socket.error as err:
             log.error("Failed to send data to OpenTSDB server: %s", err)
             try:
                 self.reconnect()
             except socket.error as err:
                 log.error("Failed reconnect to OpenTSDB server: %s", err)
     log.error("Dropping message %s", mesg)
Example #6
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     mesg = "%s %s %s\n" % (stat, value, mtime)
     for i in xrange(self.max_reconnects):
         try:
             self.sock.sendall(mesg)
             return
         except socket.error, err:
             if i+1 >= self.max_reconnects:
                 raise
             log.error("Failed to send data to Carbon server: %s" % err)
             self.reconnect()
Example #7
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     mesg = "%s %s %s\n" % (stat, value, mtime)
     for i in xrange(self.max_reconnects):
         try:
             self.sock.sendall(mesg)
             return
         except socket.error, err:
             if i + 1 >= self.max_reconnects:
                 raise
             log.error("Failed to send data to Carbon server: %s" % err)
             self.reconnect()
Example #8
0
 def convert(self, sample):
     default = self.converters["_default"]
     handler = self.converters.get(sample["plugin"], default)
     try:
         name = handler(sample)
         if name is None:
             return # treat None as "ignore sample"
     except:
         log.exception("Exception in sample handler  %s (%s):" % (
             sample["plugin"], handler))
         return
     stat = statname(sample.get("host", ""), name)
     return stat, sample["value_type"], sample["value"], int(sample["time"])
Example #9
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     mesg = "%s %s %s\n" % (stat, value, mtime)
     for i in xrange(self.max_reconnects):
         try:
             self.sock.sendall(mesg)
             return
         except socket.error as err:
             log.error("Failed to send data to Carbon server: %s", err)
             try:
                 self.reconnect()
             except socket.error as err:
                 log.error("Failed reconnect to Carbon server: %s", err)
     log.error("Dropping message %s", mesg)
Example #10
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     mesg = "%s %s %s\n" % (stat, value, mtime)
     for i in xrange(self.max_reconnects):
         try:
             self.sock.sendall(mesg)
             return
         except socket.error as err:
             log.error("Failed to send data to Carbon server: %s", err)
             try:
                 self.reconnect()
             except socket.error as err:
                 log.error("Failed reconnect to Carbon server: %s", err)
     log.error("Dropping message %s", mesg)
Example #11
0
 def convert(self, sample):
     default = self.converters["_default"]
     handler = self.converters.get(sample["plugin"], default)
     try:
         name = '.'.join(handler(sample))
         if name is None:
             return # treat None as "ignore sample"
     except:
         log.exception("Exception in sample handler  %s (%s):" % (
             sample["plugin"], handler))
         return
     host = sample.get("host", "")
     stat = names.statname(host, name, "collectd")
     #log.info("CollectDConverter.convert statname: %s" % stat);
     return host, stat, sample["value_type"], sample["value"], int(sample["time"])
Example #12
0
 def parse_metric(self, hostname, data):
     cmd, data = data[0], data[1:]
     mtype = cmd & 0xF0
     action = cmd & 0x0F
     if mtype not in self.METRIC_TYPES:
         raise ProtocolError("Invalid metric type")
     if action not in self.METRIC_ACTIONS:
         raise ProtocolError("Invalid metric action")
     name, data = self.parse_string(data)
     if action is MetricsDCommand.UPDATE:
         value, data = self.parse_number(data)
     else:
         value = None
     stat = names.statname(hostname, name.split("."))
     cmd = MetricsDCommand(stat, mtype, action, value)
     return cmd, data
Example #13
0
 def parse_metric(self, hostname, data):
     cmd, data = data[0], data[1:]
     mtype = cmd & 0xF0
     action = cmd & 0x0F
     if mtype not in self.METRIC_TYPES:
         raise ProtocolError("Invalid metric type")
     if action not in self.METRIC_ACTIONS:
         raise ProtocolError("Invalid metric action")
     name, data = self.parse_string(data)
     if action is MetricsDCommand.UPDATE:
         value, data = self.parse_number(data)
     else:
         value = None
     stat = names.statname(hostname, name.split("."))
     cmd = MetricsDCommand(stat, mtype, action, value)
     return cmd, data
Example #14
0
 def dispatch(self, host, names):
     machine = get_machine_from_uuid(host)
     if not machine:
         log.error("machine not found, wtf!")
         return
     multihandler = MultiHandler(host)
     metrics = []
     for name in names:
         target = statname(host, name)
         metric = multihandler.decorate_target(target)
         if metric['alias'].rfind("%(head)s.") == 0:
             metric['alias'] = metric['alias'][9:]
         plugin = metric['alias'].split('.')[0]
         if plugin not in self.ignore_plugins:
             metrics.append(metric)
     if not metrics:
         return
     log.info("New metrics for host %s, notifying core: %s", host, metrics)
     payload = {
         'uuid': host,
         'collectd_password': machine.collectd_password,
         'metrics': metrics,
     }
     try:
         resp = requests.post(
             "%s/new_metrics" % mon_config.CORE_URI,
             data=json.dumps(payload),
             verify=mon_config.SSL_VERIFY
         )
     except Exception as exc:
         log.error("Error notifying core: %r", exc)
         return
     if not resp.ok:
         log.error("Bad response from core: %s", resp.text)
     # also save to file in disk
     if self.fh is not None:
         try:
             for name in names:
                 self.fh.write("%s %s\n" % (host, name))
             self.fh.flush()
         except IOError as exc:
             log.error("Error writing to metrics file: %s", exc)
Example #15
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     self.buffer.append((stat, (mtime, value)))
     if len(self.buffer) >= self.buffer_size:
         self.transmit()
Example #16
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     #log.info("PickleClient send host: %s  name: %s   stat: %s" % (host, name, stat));
     self.buffer.append((stat, (mtime, value)))
     if len(self.buffer) >= self.buffer_size:
         self.transmit()
Example #17
0
 def send(self, host, name, value, mtime):
     stat = names.statname(host, name)
     self.buffer.append((stat, (mtime, value)))
     if len(self.buffer) >= self.buffer_size:
         self.transmit()