Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / sensors / voltcraft_tcm220_ @ 17f78427

Historique | Voir | Annoter | Télécharger (6,15 ko)

1
#!/usr/bin/perl -w
2
##########################################################################################################
3
#
4
# Munin plugin to monitor values reported by a Voltcraft TCM 220 or 320 temperature swicth module
5
# connected to a serial port. A cable needs to be soldered to the module's dedicated data output pads:
6
# pad no. 9 (GND) to pin no. 5 of the serial port, pad no. 17 (DATA_OUT) to pin no. 2 of the serial port
7
# and pad no. 18 (CLOCK) to pin no. 3 of the serial port. For more details, check out the documentation
8
# that came with your Voltcraft TCM 220/320 temperature swicth module.
9
#
10
# In order to use this plugin, copy it to the munin's plugin directory (eg. /usr/share/munin/plugins)
11
# under the name "voltcraft_tcm220_". Don't change this filename! Follow these steps:
12
#
13
# 1. Symlink it to munin's configured plugins directory (eg. /etc/munin/plugins) with names suggesting
14
#    the locations of the  modules you wish to monitor, eg: "voltcraft_tcm220_serverroom1".
15
#
16
#    Important: make sure to use the same names in your symlinks and other config places!
17
#
18
# 2. In /etc/munin/plugin-conf.d/munin-node add the following, to be able to contact the modules via
19
#    serial ports (obviously replacing these with your own data):
20
#
21
#    [voltcraft_tcm220_serverroom1]
22
#    user root				# Need to run the plugin as root, in order to access the RS232 port
23
#    group root
24
#    env.Port /dev/ttyS0		# Serial port where the module is connected to
25
#    env.Location Server Room 1		# Friendly name for the module location, printed on the graph
26
#    env.CH1name Ceiling		# Friendly name for the CH1 sensor location
27
#    env.CH1crit 35			# Critical temperature value which when reached, Munin should alert
28
#    env.CH2name Floor			# Friendly name for the CH2 sensor location
29
#    env.CH2crit 35			# Critical temperature value which when reached, Munin should alert
30
#
31
# 3. Restart the munin node by 'service munin-node restart'.
32
#
33
#    If all went well, after 5 minutes or so you should have tne new module's graphs listed on the Web
34
#    Interface of Munin.
35
#
36
# Note: the plugin waits maximum 11 seconds for the module to report the current temperature values to
37
# the serial port. If no value is reported, or the serial cable is unplugged, it returns Undefined to
38
# Munin. According to the documentation, the module can be configured to report values every 2 seconds
39
# instead the 10 seconds default - this can be used to speed up the data acquisition process, however
40
# it can decrease battery life (with default values, two AAA alkaline batteries last cca 2 years).
41
#
42
# Tested & working with munin v.2.0.19-2 on Ubuntu LTS 12.04.4
43
# Created in 2014 by robi
44
#  v0.2 - added checksum validation
45
#  v0.1 - initial version
46
#########################################################################################################
47
## Magic Markers
48
 #%# family=manual
49
#########################################################################################################
50
use diagnostics;
51
use strict;
52
use Device::SerialPort;
53
use warnings;
54

    
55
#########################################################################################################
56
## Receive environmentals
57
my $Portty = $ENV{'Port'};
58
my $Location = $ENV{'Location'};
59
my $CH1name = $ENV{'CH1name'};
60
my $CH1crit = $ENV{'CH1crit'};
61
my $CH2name = $ENV{'CH2name'};
62
my $CH2crit = $ENV{'CH2crit'};
63

    
64
##########################################################################################################
65
## Determine scriptname
66
my $Modname = undef;
67
$0 =~ /voltcraft_tcm220_(.+)*$/;
68
unless ($Modname = $1) {
69
print "Sorry, you did not symlink correctly the script! Please symlink as eg. voltcraft_tcm220_serverroom1\n";
70
  exit 2;
71
}
72

    
73
##########################################################################################################
74
## Configuration
75
if(exists $ARGV[0] and $ARGV[0] eq "config") {
76
  print "graph_args --base 1000\n";
77
  print "graph_title " . $Location . " temperatures\n";
78
  print "graph_vlabel degrees Celsius\n";
79
  print "graph_category sensors\n";
80
  print "graph_scale no\n";
81
  print "graph_info Measured temperature values in " . $Location . " by a Voltcraft TCM 220 temperature switch module.\n";
82
  print "ch1.label " . $CH1name . "\n";
83
  print "ch1.critical ". $CH1crit . "\n";
84
  print "ch1.info " . $CH1name . " temperature, connected to CH1.\n";
85
  print "ch2.label " . $CH2name . "\n";
86
  print "ch2.critical ". $CH2crit . "\n";
87
  print "ch2.info " . $CH2name . " temperature, connected to CH2.\n";
88
  print "\n";
89
  exit;
90
}
91

    
92
##########################################################################################################
93
## Initiate serial connection
94
my $port = Device::SerialPort->new($Portty);
95
$port->baudrate(9600);
96
$port->parity("none");
97
$port->handshake("none");
98
$port->databits(8);
99
$port->stopbits(1);
100
$port->read_char_time(0);
101
$port->read_const_time(20);
102

    
103
##########################################################################################################
104
## Execution
105
my $response = "";
106
my $resptime = (time() + 10);
107

    
108
while (1) {
109
	my $curtime = time();
110
	my ($count, $data) = $port->read(255);
111
	if ($count > 0) {
112
		$response .= $data;
113
		last if ($data // "");
114
	}
115
	last if ($curtime > $resptime);
116
}
117

    
118
my $ch1temp = "U";
119
my $ch2temp = "U";
120

    
121
if ($response // "") {
122
	my $hex = unpack 'H*', $response;
123
#	print "$hex\n";	#$hex should be something like "abc102999babc2017677" for +29.9 and +17.6
124

    
125
	my $ch1sum = substr($hex, 4, 1) + substr($hex, 5, 1) + substr($hex, 6, 1) + substr($hex, 7, 1);
126
	my $ch1sul = substr(sprintf("%02d", $ch1sum), 1, 1);
127

    
128
        my $ch2sum = substr($hex, 14, 1) + substr($hex, 15, 1) + substr($hex, 16, 1) + substr($hex, 17, 1);
129
        my $ch2sul = substr(sprintf("%02d", $ch2sum), 1, 1);
130

    
131
	if (substr($hex, 9, 1) == $ch1sul) {
132
		my $ch1z = "";
133
		if (substr($hex, 4, 1) == "5") {
134
			$ch1z ="-";
135
		}
136
		$ch1temp = $ch1z . substr($hex, 5, 2) . "." . substr($hex, 7, 1);
137
	}
138

    
139
	if (substr($hex, 19, 1) == $ch2sul) {
140
		my $ch2z = "";
141
		if (substr($hex, 14, 1) == "5") {
142
			$ch2z ="-";
143
		}
144
		$ch2temp = $ch2z . substr($hex, 15, 2) . "." . substr($hex, 17, 1);
145
	}
146
}
147

    
148
print "ch1.value $ch1temp\n";
149
print "ch2.value $ch2temp\n";
150
print "\n";
151

    
152
##########################################################################################################
153
## End