forked from deweysasser/graphiteutils
/
graphite-probe-vmware-server
executable file
·133 lines (96 loc) · 3.03 KB
/
graphite-probe-vmware-server
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl
# purpose: analyze kernel debugging output to determine which VM is
# doing the IO
use Time::Local;
use Getopt::Long;
use strict;
use vars '%Config', '@Columns';
use POSIX 'strftime', 'sysconf';
use Sys::Hostname;
$Config{date}=strftime("%s", localtime);
unless(GetOptions(
"prefix=s" => \$Config{prefix},
"column=s@" => \@Columns,
"graphite-host=s" => \$Config{graphiteHost},
)) {
die("Failed to parse options");
}
unless(defined $Config{prefix}) {
$Config{prefix} = "system." . hostname ;
}
our %IOFieldMap = (
# "rchar" => "read_chars",
# "wchar" => "write_chars",
"syscr" => "read.call_count",
"syscw" => "write.call_count",
"read_bytes" => "read.byte_count",
"write_bytes" => "write.byte_count",
);
&main;
sub main {
my $graphite;
if($Config{graphiteHost}) {
my ($host, $port) = split(/:/, $Config{graphiteHost});
$port = 2003 unless $port;
open($graphite, "|nc -q 2 $host $port") || die("Failed to netcat to $host:$port: $!");
select $graphite;
}
open(my $ps, "ps -ef | grep vmware-vmx|") || die("ps failed: $!");
while(<$ps>) {
my ($user, $pid, $parent, $c, $stime, $tty, $time, $cmd) = split(/\s+/, $_, 8);
$cmd =~ /\s(\/.*\.vmx)/;
my $vmx = $1;
next unless $vmx;
my $name = &vmxToName($vmx);
# print "# pid $pid is vmx $vmx\n";
&io($name, $pid);
&cpu($name, $pid);
}
close $ps;
close($graphite) if $graphite;
}
# read /proc/$pid/stat for a line like
# 18950 (vmware-vmx) S 1 18950 18950 0 -1 4202752 177447 1720 1106 1 95316 202333 3 0 20 0 8 0 223120114 884928512 81268 18446744073709551615 4194304 12498676 0 0 0 0 0 3149824 332001007 18446744073709551615 0 0 17 1 0 0 4765 0 0
sub cpu {
my $name = shift;
my $pid = shift;
return unless -r "/proc/$pid/stat";
open(my $stat, "/proc/$pid/stat") || die("Failed to read stat for $pid: $!");
my $clocks = sysconf(POSIX::_SC_CLK_TCK);
print "# clocks are $clocks\n";
while(<$stat>) {
chop;
my ($pidagain, $exe, $state, $ppid, $pgrp, $session, $tty_nr, $tpgid, $flags, $minflt, $cminflt, $majflt, $cmajflt, $utime, $stime, $cutime, $cstime, $pri) = split(/\s+/);
&write("vmware.vmx.cpu.$name.user.tick_count", $utime/$clocks);
&write("vmware.vmx.cpu.$name.system.tick_count", $stime/$clocks);
&write("vmware.vmx.cpu.$name.total_time.tick_count", ($utime + $stime + $cutime + $cstime)/$clocks);
}
close $stat;
}
sub io {
my $name = shift;
my $pid = shift;
return unless -r "/proc/$pid/io";
open(my $io, "/proc/$pid/io") || die("Failed to read io for $pid: $!");
while(<$io>) {
chop;
my ($field, $value) = split(/:\s+/);
my $label = $IOFieldMap{$field};
next unless $label;
&write("vmware.vmx.io.$name.$label",$value);
}
close $io;
}
sub vmxToName {
my $path = shift;
$path =~ s/\.vmx$//;
$path =~ s/\//_/g;
$path =~ s/[^a-zA-Z0-9]/_/g;
return $path;
}
sub write {
my $subkey = shift;
my $value = shift;
$value = 0 unless $value;
print "$Config{prefix}.$subkey $value $Config{date}\n";
}