forked from khorton/nas_fan_control
-
Notifications
You must be signed in to change notification settings - Fork 4
/
influxhddtemps.pl
133 lines (112 loc) · 3.11 KB
/
influxhddtemps.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
#!/usr/local/bin/perl -w
use strict;
use warnings;
my $use_influx = 1;
my $influx_fan_speed = 1;
my $influx_fan_duty = 1;
my $influx_disks = 1;
my $influx_sensors = 1;
my $influxdb_db="freenas";
my $influxdb_host="192.168.2.234";
my $influxdb_port="8086";
my $influxdb_protocol="http";
my $influxdb_hostname="fantest";
my $influxdb_url="$influxdb_protocol://$influxdb_host:$influxdb_port/write?db=$influxdb_db";
# smartctl path
my $smartctlCmd = '/usr/local/sbin/smartctl';
my @hd_list;
my @nvme_list;
main();
sub main {
@hd_list = get_hd_list();
#print @hd_list;
foreach my $disk (@hd_list) {
my $disktemp = get_one_hd_temp($disk);
}
@nvme_list = get_nvme_list();
#print @nvme_list;
foreach my $disk (@nvme_list) {
my $disktemp = get_one_nvme_temp($disk);
}
}
sub run_command {
my @cmd = @_;
my ($out, $err);
#dprint(3, 'run_command: '.join(' ', @cmd));
my $command = join(' ', @cmd);
$out = `$command`;
print $out;
# if (!run \@cmd, \undef, \$out, \$err) {
# chomp($err);
# dprint(0, "command [@cmd] failed: $err");
# die "command [@cmd] failed: $err";
# }
chomp($out);
#dprint(2, $out);
return split(/\n/, $out);
}
sub log_to_influx
{
# $type should be SensorTemp, FanSpeed, FanDuty or DiskTemp, $name should identify the item (da0, Fan 1, Temp...)
my ( $type, $name, $value) = @_;
(my $name_nospaces = $name) =~ s/\s//g;
my $data = "$type,component=$influxdb_hostname$name_nospaces value=$value";
my @command = ('curl', '-i', "-XPOST $influxdb_url -d \"$data\"");
#print join (/ /, @command), "\n";
my @output = run_command(@command);
}
sub get_hd_list {
my @cmd = ('camcontrol', 'devlist');
my @vals;
foreach (run_command(@cmd)) {
next if (/WDC|Enclosure|Virtual/);
if (/\((?:pass\d+,(a?da\d+)|(a?da\d+),pass\d+)\)/) {
#dprint(2, $1);
push(@vals, $1);
}
}
#print @vals;
return @vals;
}
sub get_nvme_list {
my @cmd = ('nvmecontrol', 'devlist');
my @vals;
foreach (run_command(@cmd)) {
next if (/WDC|Enclosure|Virtual/);
if (/(nvme[\d])\:/) {
#print $1;
push(@vals, $1);
}
}
#print @vals;
return @vals;
}
sub get_one_hd_temp
{
my $disk_dev = shift;
my @command = ($smartctlCmd, '-A', "/dev/$disk_dev");
my $temp;
foreach (run_command(@command)) {
chomp;
#print $_;
if (/Temperature_Celcius|Airflow_Temperature_Cel/) { $temp = (split)[9]; }
}
#print $temp;
if ($use_influx == 1 && $influx_disks == 1) { log_to_influx("DiskTemp", $disk_dev, $temp);}
return $temp;
}
sub get_one_nvme_temp
{
my $disk_dev = shift;
my @command = ($smartctlCmd, '-A', "/dev/$disk_dev");
my $temp;
my @result;
my $pattern = qr/Temperature\:\s*(\d+)\sCelsius/;
@result = join("\n", run_command(@command)) =~ m/$pattern/g;
if (@result) {
$temp = $result[0];
#print $temp;
if ($use_influx == 1 && $influx_disks == 1) { log_to_influx("DiskTemp", $disk_dev, $temp);}
}
return $temp;
}