-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_bsd_cputemp.pl
executable file
·150 lines (97 loc) · 2.93 KB
/
monitor_bsd_cputemp.pl
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
#!/usr/local/bin/perl
use strict;
use warnings;
use IO::Socket::INET;
use Sys::Hostname;
my %settings = (
'influxdb' => {
'address' => '192.168.20.4',
'port' => 8089,
},
);
main(\%settings);
exit;
sub main {
my $settings_ref = shift;
my $cpuinfo_ref = get_cpuinfo();
(my $result, my $message_ref) = send_data($settings_ref, $cpuinfo_ref);
print join(", ", @{$message_ref}) . "\n";
return($result);
}
sub get_cpuinfo {
my %data;
my $number_of_cpus = `/sbin/sysctl -n hw.ncpu`;
for ( my $cpu_id = 0; $cpu_id < $number_of_cpus; $cpu_id++ ) {
# Sample output
# 58.0C
my $temp = `/sbin/sysctl -n dev.cpu.$cpu_id.temperature`;
# Format the output
chomp($temp);
$temp =~ s/C$//;
$data{$cpu_id}->{'temp'} = $temp;
}
return(\%data);
}
sub create_lineprotocol {
my $data_ref = shift;
my $hostname = hostname();
my $line_protocol;
my $timestamp = (int( time /10 ) * 10 ) * 1000000000;
foreach my $cpu_id ( sort keys %{$data_ref} ) {
my @data;
my @tags = (
"cpu_temp",
"cpu_id=$cpu_id",
"host_name=$hostname",
);
push(@data, join(',', @tags));
push(@data, "value=$data_ref->{$cpu_id}->{'temp'}");
push(@data, $timestamp);
$line_protocol .= sprintf("%s %s %d\n", @data);
}
return($line_protocol);
}
sub send_data {
my $settings_ref = shift;
my $data_ref = shift;
my $socket = new IO::Socket::INET (
PeerAddr => $settings_ref->{'influxdb'}->{'address'},
PeerPort => $settings_ref->{'influxdb'}->{'port'},
Proto => 'udp',
) or return(1, [ "cannot create socket: $@" ]);
my $lineprotocol = create_lineprotocol($data_ref);
$socket->send($lineprotocol);
$socket->close;
return(0, []);
}
__END__
=head1 NAME
monitor_bsd_cputemp.pl - Monitor CPU temperatures on FreeBSD systems
=head1 VERSION
Version 1.0 (September 2016)
=head1 SYNOPSYS
monitor_bsd_cputemp.pl
=head1 DESCRIPTION
This script gathers CPU temperature information and sends it via UDP to an
Influx time-series database. The data in the InfluxDB can be visualized in
different ways like Grafana and Influx' own Chronograf.
The data is formatted specifically for the IndluxDB in the line protocol format.
The line protocol supports tags to add information to the values. The hostname
and the CPU (or core) ID are added as tags.
The address and port of the InfluxDB server is configured in the hash for the
settings at the top of the script. The influxDB database should be configured to
accept data over UDP. See the InfluxDB documentation on how to do this.
=head1 NOTES
This software has been tested on FreeNAS 9.10. It should work with other FreeBSD
versions and maybe even on other BSD versions supporting sysctl and temperature
monitoring (also depends on CPU).
=head1 AUTHOR
Jurriaan Saathof <[email protected]>
=head1 COPYRIGHT
Copyright 2016 Jurriaan Saathof
=head1 SEE ALSO
IO::Socket::INET(3pm), Sys::Hostname{3pm}
https://www.influxdata.com/time-series-platform/
http://grafana.org
http://www.freenas.org
=cut