-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsateon-config-generator.php
172 lines (135 loc) · 4.26 KB
/
sateon-config-generator.php
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
#!/usr/bin/php
<?php
# check_sateon Icinga Config Generator
#
# Generate Icinga for all sateon devices using cached device list.
#
# Run check_sateon.php once before executing this script.
# Example:
# ./check_sateon.php --hostname 10.0.1.1 --status-dc dc-fault-list.txt --list-dc dc-list.txt --device DOOR-2.14
$cfg['cache'] = "/tmp";
$cfg['config-file'] = "sateon-field-network.conf";
$cfg_list_dc = "DC.txt";
$cfg_status_dc = "dc-fault-list.txt";
$cfg_hostname = "10.0.0.1";
$cfg_username = "spectator";
$cfg_password = "secret_password";
# reset parameters
$n = 0;
$i = 0;
$cachedata = "";
$hostlist = "";
$config = <<<EOL
object CheckCommand "check_sateon" {
import "plugin-check-command"
command = [ PluginDir + "/check_sateon.php" ]
arguments = {
"--hostname" = "\$hostname\$"
"--username" = "\$username\$"
"--password" = "\$password\$"
"--status-dc" = "\$status_log\$"
"--list-dc" = "\$id_log\$"
"--device" = "\$host.name\$"
}
}
template Host "sateon-host" {
check_command = "check_sateon"
vars.hostname = "$cfg_hostname"
vars.username = "$cfg_username"
vars.password = "$cfg_password"
vars.status_dc = "$cfg_status_dc"
vars.list_dc = "$cfg_list_dc"
vars.sateon = "True"
vars.type = "RS485-Controller"
vars.owner = "Customer-AccessControl"
}
object HostGroup "RS485 Controllers" {
display_name = "RS485-Controllers"
assign where host.vars.type == "RS485-Controller"
}
apply Dependency "sateon-server" to Host {
parent_host_name = "SATEON"
disable_checks = true
disable_notifications = true
assign where host.vars.type == "RS485-Controller"
}
EOL;
# get device list from remote server
$devicelist = getContent( $cfg_hostname, $cfg_list_dc, $cfg_username, $cfg_password);
# Throw error if SystemID list cannot be updated
if (!$devicelist) {
echo "Can't get device list from server";
exit(STATUS_UNKNOWN);
}
# fix encoding to UTF-8
$devicelist = mb_convert_encoding($devicelist, 'UTF-8', 'UCS-2LE');
# parse device list to array using pattern
preg_match_all('/Comment\s+:(.+?)\nDescription\s+:(.+?)\nInterlock\s+:(.+?)\nLineId\s+:(.+?)\nMainsFailurePeriod\s+:(.+?)\nPoll\s+:(.+?)\nTimeZoneId\s+:(.+?)\nIdentity\s+:(.+?)\n/', $devicelist, $lines, PREG_PATTERN_ORDER);
# preserve subarray of device ids
$identities = $lines[8];
# iterate throught device ids
foreach ($identities as &$idstr) {
# retrieve device name from subarray 2
$dcname = $lines[2][$n];
# retrieve LH name from subarray 1
$lhname = $lines[1][$n];
# clean variables
$dcname = cleanVar($dcname);
$lhname = cleanVar($lhname);
$idstr = str_replace("AccessControl.Box:", "", $idstr);
$idstr = cleanVar($idstr);
# put id=name pair to $device hash
$lhline[$lhname] = <<<ELH
object HostGroup "$lhname" {
display_name = "$lhname"
assign where host.vars.lineheader == "$lhname"
}
apply Dependency "sateon-$lhname" to Host {
parent_host_name = "SATEON-$lhname"
disable_checks = true
disable_notifications = true
assign where host.vars.lineheader == "$lhname"
}
ELH;
$config_line = <<<EOL
object Host "$dcname" {
import "sateon-host"
vars.description = "SATEON panel $dcname"
vars.lineheader = "$lhname"
vars.systemid = "$idstr"
}
EOL;
$hostlist = $hostlist.$config_line;
# raise counter
$n++;
}
$lhlist = implode("\n", $lhline);
$config = $config.$lhlist.$hostlist;
# compose config url
$configname = $cfg['cache']."/".$cfg['config-file'];
file_put_contents($configname, $config);
# remove line breaks and leading spaces
function cleanVar ($variable) {
$variable = str_replace("\n", "", $variable);
$variable = str_replace("\r", "", $variable);
$variable = preg_replace("/^\s/", "", $variable);
return $variable;
}
# fetch data from remote server
function getContent ($host, $path, $username, $password) {
ini_set('default_socket_timeout', 3);
$params = array(
'http' => array(
'method' => "GET",
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
);
$ctx = stream_context_create($params);
$url = "http://$host/$path";
$data = file_get_contents($url, false, $ctx);
if ($data === false) {
return 0;
}
return $data;
}
?>