Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

System monitor.

  • Loading branch information...
commit 051dd6549718652052e795fe8c7a096c27fa5c09 1 parent bd88028
@dhruvbansal dhruvbansal authored
Showing with 75 additions and 0 deletions.
  1. +1 −0  lib/graphiterb/monitors.rb
  2. +74 −0 lib/graphiterb/monitors/system.rb
View
1  lib/graphiterb/monitors.rb
@@ -2,6 +2,7 @@ module Graphiterb
module Monitors
autoload :DiskSpace, 'graphiterb/monitors/disk_space'
+ autoload :System, 'graphiterb/monitors/system'
autoload :DirectoryTree, 'graphiterb/monitors/directory_tree'
# Accepts a lightweight call every iteration.
View
74 lib/graphiterb/monitors/system.rb
@@ -0,0 +1,74 @@
+module Graphiterb
+ module Monitors
+ class System < Graphiterb::Monitors::PeriodicMonitor
+
+ def top
+ `top -b -n3`.chomp.split(/^top -/).last.split("\n") rescue []
+ end
+
+ def df
+ `/bin/df`.chomp.split("\n").
+ grep(%r{^/dev/}).
+ map{|line| line.split(/\s+/) } rescue []
+ end
+
+ def cpu lines
+ cpus = 0.0
+ total_percentage = 0.0
+ lines.each do |line|
+ next unless line =~ /^Cpu.* *: *([\d\.]+)%us/
+ cpus += 1.0
+ total_percentage += $1.to_f
+ end
+ total_percentage / cpus rescue 0.0
+ end
+
+ def processes lines
+ lines.each do |line|
+ next unless line =~ /^Tasks: *(\d+) *total, *(\d+) *running/
+ return [$1, $2].map(&:to_i)
+ end
+ [0, 0]
+ end
+
+ def memory lines
+ lines.each do |line|
+ next unless line =~ /^Mem: *\d+k *total, *(\d+)k *used, *(\d+)k *free/
+ return [$1, $2].map(&:to_i)
+ end
+ [0,0]
+ end
+
+ def swap lines
+ lines.each do |line|
+ next unless line =~ /^Swap: *\d+k *total, *(\d+)k *used, *(\d+)k *free/
+ return [$1, $2].map(&:to_i)
+ end
+ [0,0]
+ end
+
+
+ def get_metrics metrics, since
+ puts '=' * 80
+ df.each do |handle, size, spaceused, spacefree, percentfree, location|
+ disk_name = handle.gsub(/^\//, '').split('/')
+ metrics << [scope(hostname, disk_name, 'available'), spacefree.to_i]
+ end
+
+ lines = top
+
+ metrics << [scope(hostname, 'cpu', 'avg_usage'), cpu(lines)]
+ metrics << [scope(hostname, 'processes', 'count'), processes(lines)]
+
+ mem_used, mem_free = memory(lines)
+ swap_used, swap_free = swap(lines)
+
+ metrics << [scope(hostname, 'memory', 'used'), mem_used ]
+ metrics << [scope(hostname, 'memory', 'free'), mem_free ]
+ metrics << [scope(hostname, 'swap', 'used'), swap_used]
+ metrics << [scope(hostname, 'swap', 'free'), swap_free]
+ end
+
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.