Custom Collectd plugins for Linux/Ubuntu
About Collectd
Collectd is an application which collects system/application metrics periodically and provides ways to store these values in variety of ways like time series databases. One of the most popular usecase is collecting metrics from Linux machines and send data to time series databases like graphite, influxdb ..
Plugins for collectd
In this article i will explain how to write custom collectd plugins. We write a plugin for gathering the metric “number of openfiles” in the Linux system”.
Step 1. Create the Plugin
We create a script which collect the openfiles count from the file /proc/sys/fs/file-nr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#!/usr/bin/env ruby
PLUGIN_NAME = ‘totalopenfile’
hostname = ENV[‘COLLECTD_HOSTNAME’] || “localhost”
interval = ENV[‘COLLECTD_INTERVAL’] || 20
def usage
puts(“#{$0} -h [-i ]”)
exit
end
# Main
begin
# Sync stdout so that it will flush to collectd properly.
$stdout.sync = true
# Collection loop
while true do
start_run = Time.now.to_i
# collect data and print the values
data = cat /proc/sys/fs/file-nr | awk '{print $1}'
puts(“PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-openfiles #{start_run}:#{data}”)
exit
end
end
|
Step 2 : Install the new Collectd custom plugin to Collectd
Copy the script we created from Step 1 , to the collectd plugin folder. On Centos its at /usr/lib64/collectd/
Step 3 : Configure new Plugin
Now we will link the new plugin into collectd.conf file.
1 2 3 4 | LoadPlugin exec Exec “collectd” “/usr/lib64/collectd/plugins/totalopenfiles.rb” |
NB: You need to create the user “collectd” if it doesnt exist
Step4 : Restart Collectd Service
On Centos7, use
1 2 3 | systemctl start collectd |
On Centos6 use,
1 2 3 | service collectd start |
Watch the Collectd logs for any errors. On Linux machines, using
1 2 3 | tail /var/log/messages |
On Ubuntu, use
1 2 3 | tail –f /var/log/syslog |
It should show the new plugin details in the logs and collected metrics will be send to the destination configured, network, graphite..If any errors, it will show up un the logs. Please let me know incase you face any issue, always glad to assist.