Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

System monitor.

  • Loading branch information...
commit 051dd6549718652052e795fe8c7a096c27fa5c09 1 parent bd88028
Dhruv Bansal dhruvbansal authored
1  lib/graphiterb/monitors.rb
@@ -2,6 +2,7 @@ module Graphiterb
2 2 module Monitors
3 3
4 4 autoload :DiskSpace, 'graphiterb/monitors/disk_space'
  5 + autoload :System, 'graphiterb/monitors/system'
5 6 autoload :DirectoryTree, 'graphiterb/monitors/directory_tree'
6 7
7 8 # Accepts a lightweight call every iteration.
74 lib/graphiterb/monitors/system.rb
... ... @@ -0,0 +1,74 @@
  1 +module Graphiterb
  2 + module Monitors
  3 + class System < Graphiterb::Monitors::PeriodicMonitor
  4 +
  5 + def top
  6 + `top -b -n3`.chomp.split(/^top -/).last.split("\n") rescue []
  7 + end
  8 +
  9 + def df
  10 + `/bin/df`.chomp.split("\n").
  11 + grep(%r{^/dev/}).
  12 + map{|line| line.split(/\s+/) } rescue []
  13 + end
  14 +
  15 + def cpu lines
  16 + cpus = 0.0
  17 + total_percentage = 0.0
  18 + lines.each do |line|
  19 + next unless line =~ /^Cpu.* *: *([\d\.]+)%us/
  20 + cpus += 1.0
  21 + total_percentage += $1.to_f
  22 + end
  23 + total_percentage / cpus rescue 0.0
  24 + end
  25 +
  26 + def processes lines
  27 + lines.each do |line|
  28 + next unless line =~ /^Tasks: *(\d+) *total, *(\d+) *running/
  29 + return [$1, $2].map(&:to_i)
  30 + end
  31 + [0, 0]
  32 + end
  33 +
  34 + def memory lines
  35 + lines.each do |line|
  36 + next unless line =~ /^Mem: *\d+k *total, *(\d+)k *used, *(\d+)k *free/
  37 + return [$1, $2].map(&:to_i)
  38 + end
  39 + [0,0]
  40 + end
  41 +
  42 + def swap lines
  43 + lines.each do |line|
  44 + next unless line =~ /^Swap: *\d+k *total, *(\d+)k *used, *(\d+)k *free/
  45 + return [$1, $2].map(&:to_i)
  46 + end
  47 + [0,0]
  48 + end
  49 +
  50 +
  51 + def get_metrics metrics, since
  52 + puts '=' * 80
  53 + df.each do |handle, size, spaceused, spacefree, percentfree, location|
  54 + disk_name = handle.gsub(/^\//, '').split('/')
  55 + metrics << [scope(hostname, disk_name, 'available'), spacefree.to_i]
  56 + end
  57 +
  58 + lines = top
  59 +
  60 + metrics << [scope(hostname, 'cpu', 'avg_usage'), cpu(lines)]
  61 + metrics << [scope(hostname, 'processes', 'count'), processes(lines)]
  62 +
  63 + mem_used, mem_free = memory(lines)
  64 + swap_used, swap_free = swap(lines)
  65 +
  66 + metrics << [scope(hostname, 'memory', 'used'), mem_used ]
  67 + metrics << [scope(hostname, 'memory', 'free'), mem_free ]
  68 + metrics << [scope(hostname, 'swap', 'used'), swap_used]
  69 + metrics << [scope(hostname, 'swap', 'free'), swap_free]
  70 + end
  71 +
  72 + end
  73 + end
  74 +end

0 comments on commit 051dd65

Please sign in to comment.
Something went wrong with that request. Please try again.