forked from talsonthomas/nagios-nconf-new
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_generate_config.php
285 lines (242 loc) · 10.6 KB
/
exec_generate_config.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
#This is just a commented line
#This is just a commented line
require_once 'main.php';
# Remove lock file if process fails
$lock_file = 'temp/generate.lock';
function remove_lock(){
# remove lock file
global $lock_file;
$unlink_status = unlink($lock_file);
if (!$unlink_status){
NConf_DEBUG::set('removing old lock failed', 'ERROR');
}
}
history_add("general", "config", "generating...");
// predefine status as OK
$status = "OK";
// check if "temp" dir is writable
if(!is_writable(NCONFDIR."/temp/")){
$content = "Could not write to 'temp' folder. Cannot generate config.";
NConf_DEBUG::set($content, 'ERROR');
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error')
);
remove_lock();
exit;
}
// check if "output" dir is writable
if(!is_writable(NCONFDIR."/output/")){
$content = "Could not write to 'output' folder. Cannot store generated config.";
NConf_DEBUG::set($content, 'ERROR');
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error')
);
remove_lock();
exit;
}
// check if generate_config script is executable
if(!is_executable(NCONFDIR."/bin/generate_config.pl")){
$content = "Could not execute generate_config script. <br>The file '".NCONFDIR."/bin/generate_config.pl' is not executable.";
NConf_DEBUG::set($content, 'ERROR');
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error')
);
remove_lock();
exit;
}
// check if the Nagios / Icinga binary is executable
exec(NAGIOS_BIN,$bin_out);
if(!preg_match('/Nagios|Icinga/',implode(' ',$bin_out))){
$content = "Error accessing or executing Nagios / Icinga binary '".NAGIOS_BIN."'. <br>Cannot run the mandatory syntax check.";
NConf_DEBUG::set($content, 'ERROR');
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error')
);
remove_lock();
exit;
}
// check if existing "output/NagiosConfig.tgz" is writable
if(file_exists(NCONFDIR."/output/NagiosConfig.tgz" and !is_writable(NCONFDIR."/output/NagiosConfig.tgz"))){
$content = "Cannot rename ".NCONFDIR."/output/NagiosConfig.tgz. Access denied.";
NConf_DEBUG::set($content, 'ERROR');
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error')
);
remove_lock();
exit;
}
// check if static config folder(s) are readable
foreach ($STATIC_CONFIG as $static_folder){
if(!is_readable($static_folder)){
$content = "<br>Could not access static config folder '".$static_folder."'.";
$content .= "<br>Check your \$STATIC_CONFIG array in 'config/nconf.php'.";
NConf_DEBUG::set($content, 'ERROR');
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error')
);
remove_lock();
exit;
}
}
// fetch all monitor and collector servers from DB
$servers = array();
$query = "SELECT fk_id_item AS item_id,attr_value,config_class
FROM ConfigValues,ConfigAttrs,ConfigClasses
WHERE id_attr=fk_id_attr
AND naming_attr='yes'
AND id_class=fk_id_class
AND (config_class = 'nagios-collector' OR config_class = 'nagios-monitor')
ORDER BY attr_value";
$result = db_handler($query, "result", "fetch all monitor and collector servers from DB");
while ($entry = mysql_fetch_assoc($result) ){
$renamed = preg_replace('/-|\s/','_',$entry["attr_value"]);
if($entry["config_class"] == 'nagios-collector'){
$renamed = preg_replace('/Nagios|Icinga/i','collector',$renamed);
}
array_push($servers, $renamed);
}
# GENERATE CONFIG
echo NConf_HTML::title('Generate config log:');
echo '<div>
<pre>';
//system(NCONFDIR."/bin/generate_config.pl");
$command = NCONFDIR."/bin/generate_config.pl";
$output = array();
exec($command, $output);
// print each line
foreach ($output AS $line){
// Filter some lines:
if ( empty($line)) continue;
if ( strpos($line, "Copyright")) continue;
if ( strpos($line, "Initializing")) continue;
// Look for error
if ( strstr($line, "ERROR") ){
$status = "error";
echo '<br><span class="red">'.$line.'</span>';
}elseif ( strstr($line, "WARN") ){
echo '<br><span class="orange">'.$line.'</span>';
}else{
echo "<br>$line";
}
}
echo '</pre>
</div>';
// create tar file
system("cd ".NCONFDIR."/temp; tar -cf NagiosConfig.tar global ".implode(" ", $servers));
// add folders with static config to tar file
foreach ($STATIC_CONFIG as $static_folder){
if(!is_empty_folder($static_folder) and is_empty_folder($static_folder) != "error"){
$last_folder = basename($static_folder);
system("cd ".$static_folder."; cd ../; tar -rf ".NCONFDIR."/temp/NagiosConfig.tar ".$last_folder);
}
}
// compress tar file
system("cd ".NCONFDIR."/temp; gzip NagiosConfig.tar; mv NagiosConfig.tar.gz NagiosConfig.tgz");
echo '<br><br>';
echo NConf_HTML::title('Running syntax check:');
//$icon_count = 1;
echo '<div class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons ui-nconf-accordion-list">';
### SYNTAX CHECK
# now run tests on all generated files
$details = '';
$break = ' - ';
foreach ($servers as $server){
$server_str = preg_replace("/\./", "_", $server);
# run test
exec(NAGIOS_BIN." -v ".NCONFDIR."/temp/test/".$server.".cfg",$srv_summary[$server]);
$total_msg = '';
$count=0;
$i = 0;
foreach($srv_summary[$server] as $line){
if( preg_match("/^Total/",$line) ){
# add splitter between messages
$total_msg .= ( $i > 0 ) ? $break : '';
$i++;
$total_msg .= $line;
$count++;
if( preg_match("/Errors/",$line) && !preg_match('/Total Errors:\s+0/',$line)){
$status = "error";
}
}
}
if($count==0){
$total_msg .= "Error generating config";
$status = "error";
}
$total_msg = '<span class="notBold accordion_header_right">'.$total_msg.'</span>';
// print server info
$title = '<span class="ui-icon ui-icon-triangle-1-e"></span><a href="#">'.$server_str.$total_msg.'</a>';
echo NConf_HTML::title($title, 3, 'class="accordion_title ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom"');
echo '<div class="accordion_content ui-widget-content ui-corner-bottom monospace" style="display: none;">';
foreach($srv_summary[$server] as $line){
if ( preg_match("/^Error:/",$line) ){
echo '<span class="red">'.$line.'</span>';
}elseif ( preg_match("/^Warning:/",$line) ){
echo '<span class="orange">'.$line.'</span>';
}else{
echo $line;
}
echo '<br>';
}
echo '<br>';
echo '</div>';
}
echo '</div><br>';
if($status == "OK"){
history_add("general", "config", "generated successfully");
// Move generated config to "output" dir
if(file_exists(NCONFDIR."/output/NagiosConfig.tgz")){
system("mv ".NCONFDIR."/output/NagiosConfig.tgz ".NCONFDIR."/output/NagiosConfig.tgz.".time());
}
system("mv ".NCONFDIR."/temp/NagiosConfig.tgz ".NCONFDIR."/output/");
system("rm -rf ".NCONFDIR."/temp/*");
if(ALLOW_DEPLOYMENT == 1){
echo NConf_HTML::title('Deploy generated config:');
// check if new deployment is configured
$deployment_config = NCONFDIR.'/config/deployment.ini';
$deployment_info = FALSE;
if ( !file_exists($deployment_config) ){
$deployment_info = TRUE;
}elseif( is_readable($deployment_config) ){
$ini_array = parse_ini_file($deployment_config, TRUE);
if ( empty($ini_array) ){
$deployment_info = TRUE;
}
}
if ($deployment_info){
$content = 'The generated configuration has been written to the "nconf/output/" directory.<br>
To set up more sophisticated deployment functionality, please edit your "config/deployment.ini" file accordingly.<br>
For a complete list of available deployment options, refer to the online documentation on
<a href="http://www.nconf.org" target="_blank">www.nconf.org</a>.';
system("bash /var/www/nconf/ADD-ONS/deploy_local.sh");
echo NConf_HTML::limit_space(
NConf_HTML::show_highlight('Note', $content)
);
}else{
// Show deployment button
echo "<form method=\"POST\" action=\"call_file.php?module_file=deployment/main.php\" id=buttons>";
echo '<input type=hidden name=status value="'.$status.'">';
echo '<br><input type="submit" name="submit" value="Deploy" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">';
echo "</form><br>";
}
}else{
// Simply show success message
echo "<b>Changes updated successfully.</b><br><br>";
}
}else{
history_add("general", "config", "generate failed with syntax errors");
// Remove generated config - syntax check has failed
if(DEBUG_MODE == 1){
// Move generated config to "output" dir, but tag it as FAILED
system("mv ".NCONFDIR."/temp/NagiosConfig.tgz ".NCONFDIR."/output/NagiosConfig_FAILED.tgz.".time());
}
// Remove generated config
system("rm -rf ".NCONFDIR."/temp/*");
$content = "Deployment not possible due to errors in configuration.";
echo NConf_HTML::limit_space(
NConf_HTML::show_error('Error', $content)
);
}
mysql_close($dbh);
?>