def run(sock, delay): """Make the client go go go""" while True: # Epoch, timestamp in seconds since 1970 now = int(time.time()) # Initialize the protobuf payload payload_pb = Payload() labels = ['1min', '5min', '15min'] for name, value in zip(labels, os.getloadavg()): m = payload_pb.metrics.add() m.metric = 'system.loadavg_' + name p = m.points.add() p.timestamp = now p.value = value print("sending message") print(('-' * 80)) print(payload_pb) package = payload_pb.SerializeToString() # The message must be prepended with its size size = struct.pack('!L', len(package)) sock.sendall(size) # Then send the actual payload sock.sendall(package) time.sleep(delay)
def _sendDatapointsNow(self, datapoints): metrics = {} payload_pb = Payload() for metric, datapoint in datapoints: if metric not in metrics: metric_pb = payload_pb.metrics.add() metric_pb.metric = metric metrics[metric] = metric_pb else: metric_pb = metrics[metric] point_pb = metric_pb.points.add() point_pb.timestamp = int(datapoint[0]) point_pb.value = datapoint[1] self.sendString(payload_pb.SerializeToString())
def decode_sent(data): pb_size = unpack(INT32_FORMAT, data[:INT32_SIZE])[0] data = data[INT32_SIZE:INT32_SIZE + pb_size] datapoints = [] payload_pb = Payload.FromString(data) for metric_pb in payload_pb.metrics: for point_pb in metric_pb.points: datapoints.append( (metric_pb.metric, (point_pb.timestamp, point_pb.value))) return datapoints
def stringReceived(self, data): try: payload_pb = Payload.FromString(data) except DecodeError: log.listener('invalid protobuf received from %s, ignoring' % self.peerName) return for metric_pb in payload_pb.metrics: for point_pb in metric_pb.points: self.metricReceived( metric_pb.metric, (point_pb.timestamp, point_pb.value))