Skip to content

Instantly share code, notes, and snippets.

@GedowFather
Last active April 30, 2016 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GedowFather/be3b00611330d3c603d5 to your computer and use it in GitHub Desktop.
Save GedowFather/be3b00611330d3c603d5 to your computer and use it in GitHub Desktop.
AWS Lambda python script for sending metric value to collectd server.
#
# For sending metric value to collectd server.
#
# Usage:
# from modules.CollectdClient import CollectdClient
# COLLECTD_HOST = "collectd.example.com"
# COLLECTD_PORT = 25826
# client = CollectdClient(COLLECTD_HOST, COLLECTD_PORT)
# client.putval('example-web-01', 'service', 'gauge', 'metric', 300, 1.2345)
#
import socket
import struct
import time
class CollectdClient:
log = True
def __init__(self, host='127.0.0.1', port=25826):
self.address = (host, port)
self.__set_codes()
self.__connect()
def __log(self, log):
if self.log: print log
def __set_codes(self):
self.type_codes = {
'host' : 0x0000,
'time' : 0x0001,
'plugin' : 0x0002,
'plugin_instance' : 0x0003,
'type' : 0x0004,
'type_instance' : 0x0005,
'values' : 0x0006,
'interval' : 0x0007,
}
self.long_int_codes = [
self.type_codes['time'], self.type_codes['interval']
]
self.string_codes = [
self.type_codes['host'],
self.type_codes['plugin'], self.type_codes['plugin_instance'],
self.type_codes['type'], self.type_codes['type_instance'],
]
self.value_codes = {
'counter' : 0,
'gauge' : 1,
'derive' : 2,
'absolute' : 3,
}
self.value_strings = {
'counter' : "!Q",
'gauge' : "<d",
'derive' : "!q",
'absolute' : "!Q",
}
def __connect(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except Exception as e:
raise Exception("Can't connect server.\nERROR: %s" % e)
def __message(self, host, plugin, type, type_instance, interval, value):
start = "".join([
self.__pack(type, self.type_codes['host'], host),
self.__pack(type, self.type_codes['time'], time.time()),
self.__pack(type, self.type_codes['plugin'], plugin),
self.__pack(type, self.type_codes['plugin_instance'], ""),
self.__pack(type, self.type_codes['type'], type),
self.__pack(type, self.type_codes['interval'], interval),
])
part = self.__pack(type, type_instance, value)
return "".join([start, part])
def __pack(self, type, id, value):
if isinstance(id, basestring): #Value
return "".join([
self.__pack(type, self.type_codes['type_instance'], id),
struct.pack("!HHH", self.type_codes['values'], 15, 1),
struct.pack("<B", self.value_codes[type]),
struct.pack(self.value_strings[type], value),
])
elif id in self.long_int_codes: #Numeric
return struct.pack("!HHq", id, 12, int(value))
elif id in self.string_codes: #String
value = str(value)
return struct.pack("!HH", id, 5 + len(value)) + value + "\0"
else:
raise AssertionError("Invalid type code " + str(id))
# This options is same as
# "PUTVAL %s/%s/%s-%s interval=%d N:%f" % (host, plugin, type, type_instance, interval, value)
# within collectd exec plugin
def putval(self, host, plugin, type, type_instance, interval, value):
if type != "gauge": value = int(value)
vars = (host, plugin, type, type_instance, interval, value)
self.__log("PUTVAL %s/%s/%s-%s interval=%d N:%s" % vars)
message = self.__message(*vars)
self.sock.sendto(message, self.address)
if __name__ == '__main__':
client = CollectdClient()
client.putval('example-web-01', 'service', 'gauge', 'metric', 300, 1.2345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment