summaryrefslogtreecommitdiff
path: root/files/munin/ekeyd_stat_
blob: 43a7c4757022665f564121405da66380051dcb73 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/perl -w
#
# Entropy Key statistic reporting plugin for munin
#
# use by soft linking the script to a ekey statistic
# for example ln -s /usr/share/munin/ekeyd_stat_ ekeyd_stat_KeyTemperatureC
# will give a graph of each entropy keys temperature in Celsius
#
# for example ln -s /usr/share/munin/ekeyd_stat_ ekeyd_stat_total_EntropyRate
# will give a graph of the total entropy rate from all keys in bits per second
#
# The plugin.conf.d/munin-node must have a stanza [ekeyd_*] with user root in 
#  it as the plugin requires root access to aquire the statistics
#
# Copyright 2009 Simtec Electronics
#
# For licence terms refer to the COPYING file.

# Magic markers for munin
#%# family=auto
#%# capabilities=autoconf suggest

use strict;

use Socket;
use IO::Handle;

my $control_sock = exists $ENV{controlsocket} ? $ENV{controlsocket} : '/var/run/ekeyd.sock';

# mappings to make output prettier
my %titles = ("KeyTemperatureC", "Temperature" ,"KeyTemperatureF", "Temperature", "KeyTemperatureK" ,  "Temperature" , "TotalEntropy",  "Entropy Rate", "KeyVoltage", "Supply Voltage", "FipsFrameRate", "Fips Frame Rate", "EntropyRate", "Entropy Rate");
my %graph_axis = ( "KeyTemperatureC", "Celsius", "KeyTemperatureF", "Fahrenheit", "KeyTemperatureK", "Kelvin" , "EntropyRate", "Bits per second" , "TotalEntropy", "Bytes per second" , "KeyVoltage", "Volts", "ConnectionTime", "Seconds", "FipsFrameRate", "Frames per second");
my %graph_type = ( "TotalEntropy" , "DERIVE", "BytesRead" , "COUNTER", "BytesWritten" , "COUNTER", "ConnectionPackets" , "COUNTER" );
my %graph_min = ( "TotalEntropy" , 0 );

sub ekeyd_connect {
    my ($rendezvous) = @_;
    my $line;
    my $sock;

    socket($sock, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
    connect($sock, sockaddr_un($rendezvous)) || die "connect: $!";

    $line = <$sock>;
    if ((!defined($line)) || ($line ne "PROTOCOL EKEYD/1\n")) {
	die "Unrecognised EKEYD " . $line;
    }

    return $sock;
}

# issues a command to the ekeyd and retrieves the results
sub ekeyd_command {
    my ($sock, $command, @params) = @_;
    my @lines;
    my $line;
    my $pnum = scalar @params;

    if ($pnum > 0) {
	my $pcnt = 0;
	$command .= "(";
	while ($pcnt < $pnum) {
	    $command = $command . "\"" . $params[$pcnt] . "\"";
	    $pcnt++;
	    if ($pcnt == $pnum) {
		$command .= ")";
	    } else {
		$command .= ",";
	    }
	}
    }

    print $sock $command . "\n";
    $sock->flush;

    push @lines, $line while ((defined($line = <$sock>)) and $line ne "OK\n" and $line !~ "^ERROR.*");

    chomp @lines;

    return @lines;
}

# discover if plugin can actually be used on this system
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
    if ($control_sock and -S $control_sock) {
        print "yes\n";
	exit 0;
    } else {
	print "no (Control socket $control_sock not found)\n";
	exit 1;
    }
}

# suggest appropriate default links
if ( defined $ARGV[0] and $ARGV[0] eq "suggest" ) {
    print "total_TotalEntropy\n";
    print "KeyTemperatureC\n";
    exit 0;
}

# aquire the name of the statistic to monitor.
$0 =~ /ekeyd_stat_total_(.+)*$/; 
my $statistic = $1;
my $total_flag = 1;
if (!defined($statistic)) {
    $0 =~ /ekeyd_stat_(.+)*$/; 
    $statistic = $1;
    $total_flag = 0;
    if (!defined($statistic)) {
	die "A statistic must be provided";
    }
}

# connect to the ekeyd command socket
my $SOCKET = ekeyd_connect($control_sock);

# find all the entropy keys attached
my @result = ekeyd_command($SOCKET, "ListEntropyKeys");

# remove header line
shift @result; 

if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {

    # work out graph title
    my $title;
    if (defined $titles{$statistic}) {
	$title = $titles{$statistic};
    } else {
	$title = $statistic;
    }

    if ($total_flag == 1) {
	if (scalar(@result) < 2) {
            print "graph_title Entropy Key " . $title . "\n";
	} else {
            print "graph_title Entropy Key Combined " . $title . "\n";
	}
    } else {
        print "graph_title Entropy Key " . $title . "\n";
    }

    # label the axis as apropriate
    if (defined $graph_axis{$statistic}) {
	print "graph_vlabel " . $graph_axis{$statistic} . "\n";
    }
 
    print "graph_category sensors\n";

    if ($total_flag == 1) {
	if (scalar(@result) < 2) {
	    print "totstat.label $title\n";
	} else {
	    print "totstat.label Combined $title for " . scalar(@result) . " Entropy Keys\n";
	}

	# set the graph type
	if (defined $graph_type{$statistic}) {
	    print "totstat.type " . $graph_type{$statistic} . "\n";
	} else {
	    print "totstat.type GAUGE\n";
	}

	#set the graph minimum
	if (defined $graph_min{$statistic}) {
            print "totstat.min " . $graph_min{$statistic} . "\n";
	}
    } else {
	# details for each key 
	foreach my $keyline (@result) {
	    my @elmnt = split(/\t/, $keyline);
	    my $name = $elmnt[5];
	    $name =~ s,/,_,g;
	    print "stats" . $name . ".label " . $elmnt[5] . "\n";

	    # set the graph type
	    if (defined $graph_type{$statistic}) {
		print "stats" . $name . ".type " . $graph_type{$statistic} . "\n";
	    } else {
		print "stats" . $name . ".type GAUGE\n";
	    }

	    #set the graph minimum
	    if (defined $graph_min{$statistic}) {
                print "stats". $elmnt[5] . ".min " . $graph_min{$statistic} . "\n";
	    }
	}
    }
} else {
    my $total = 0;
    foreach my $keyline (@result) {

	# split up the result line
	my @elmnt = split(/\t/, $keyline);

	# get the status of the entropy key 
	my @stat_res = ekeyd_command($SOCKET, "StatEntropyKey", $elmnt[5]);

	my $tmp;
	my %key_stats;

	foreach $tmp (@stat_res) {
	    my @keyval = split(/\t/, $tmp);
	    @keyval = split(/=/, $keyval[1]);
	    $key_stats{$keyval[0]} = $keyval[1];
	}
	$total += $key_stats{$statistic};

	if ($total_flag == 0) {
	    print "stats" . $elmnt[5] . ".value " . $key_stats{$statistic} . "\n";
	}
    }
    if ($total_flag == 1) {
	if (scalar(@result) < 1) {
	    $total = "U";
	}
	print "totstat.value " . $total . "\n";
    }
}

close $SOCKET;

exit 0;