Created
November 7, 2017 18:25
-
-
Save brownjohnf/0a4efef042629700b183c5f0e56f4b44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from checks import AgentCheck | |
from datadog import statsd | |
import os | |
PROC_PATH = "/host/proc" | |
#sysctl -n net.ipv4.tcp_mem | |
max_tcp_mem = int(os.environ["MAX_TCP_MEM"].split()[2]) | |
#sysctl -n net.ipv4.udp_mem | |
max_udp_mem = int(os.environ["MAX_UDP_MEM"].split()[2]) | |
#sysctl -n net.ipv4.tcp_max_orphans | |
max_tcp_orphan = int(os.environ["MAX_TCP_ORPHANS"].split()[0]) | |
def sockstat(): | |
sockstat_dict = {} | |
proc_sockstat = open(os.path.join(PROC_PATH,'net/sockstat')).read().split("\n") | |
for line in proc_sockstat: | |
elements = line.split() | |
if len(elements) > 0: | |
protocol = elements[0].split(":")[0] | |
sockstat_dict[protocol] = {} | |
for i in range (1,len(elements) , 2): | |
sockstat_dict[protocol][elements[i]] = int(elements[i+1]) | |
return sockstat_dict | |
class CustomCheck(AgentCheck): | |
def check(self, instance): | |
sockstat_dict = sockstat() | |
tcp_mem = sockstat_dict["TCP"]["mem"] | |
udp_mem = sockstat_dict["UDP"]["mem"] | |
tcp_orphan = sockstat_dict["TCP"]["orphan"] | |
statsd.gauge('custom.tcp_mem', tcp_mem) | |
statsd.gauge('custom.tcp_mem.in_use', tcp_mem*1.0/max_tcp_mem) | |
statsd.gauge('custom.tcp_orphan', tcp_orphan) | |
statsd.gauge('custom.tcp_orphan.in_use', tcp_orphan*1.0/max_tcp_orphan) | |
statsd.gauge('custom.udp_mem', udp_mem) | |
statsd.gauge('custom.udp_mem.in_use', udp_mem*1.0/max_udp_mem) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment