Skip to content

Commit 65a4510

Browse files
committed
Support for ShutDown & Up scripts to start/stop icinga
1 parent b18547b commit 65a4510

7 files changed

Lines changed: 429 additions & 116 deletions

File tree

nbproject/private/private.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
<group name="Monitoring">
1111
<file>file:/home/vitex/Projects/VitexSoftware/iciedit/src/includes/Configure.php</file>
1212
</group>
13-
<group>
14-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/classes/UI/MainMenu.php</file>
15-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/debian/install</file>
16-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/classes/Script.php</file>
17-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/cfgconfirm.php</file>
18-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/index.php</file>
19-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/downtime.php</file>
20-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/classes/ExternalCommand.php</file>
21-
<file>file:/home/vitex/Projects/VitexSoftware/icinga_configurator/src/script.php</file>
22-
</group>
13+
<group/>
2314
</open-files>
2415
</project-private>

src/classes/NSCPConfigBatGenerator.php

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
*/
1010
class NSCPConfigBatGenerator extends \Ease\Atom
1111
{
12+
/**
13+
* Files in this format Suffix
14+
* @var comment
15+
*/
16+
public $formatSuffix = 'bat';
17+
18+
/**
19+
* Comment mark
20+
* @var string
21+
*/
22+
public $comment = 'REM';
23+
1224
/**
1325
* Objekt hostu
1426
* @var Engine\Host
@@ -25,7 +37,7 @@ class NSCPConfigBatGenerator extends \Ease\Atom
2537
* Config file fragments
2638
* @var array
2739
*/
28-
public $nscBatArray = [];
40+
public $nscConfArray = [];
2941

3042
/**
3143
* Active mode
@@ -49,13 +61,19 @@ class NSCPConfigBatGenerator extends \Ease\Atom
4961
* How to call %NSCLIENT variable ?
5062
* @var string
5163
*/
52-
private $nscvar = '';
64+
protected $nscvar = '';
5365

5466
/**
5567
* Scripts to deploy listing
5668
* @var array
5769
*/
58-
private $scriptsToDeploy = [];
70+
public $scriptsToDeploy = [];
71+
72+
/**
73+
* List of script filenames to deploy
74+
* @var array
75+
*/
76+
public $scriptsToDeployNames = [];
5977

6078
/**
6179
* NSC++ Configuration Generator
@@ -92,9 +110,9 @@ public function __construct($host)
92110
* @param string $key
93111
* @param string $value
94112
*/
95-
private function addCfg($path, $key, $value)
113+
protected function addCfg($path, $key, $value)
96114
{
97-
$this->nscBatArray[] = "\n".$this->nscvar.' settings --path "'.$path.'" --key "'.$key.'" --set "'.$value.'"';
115+
$this->nscConfArray[] = "\n".$this->nscvar.' settings --path "'.$path.'" --key "'.$key.'" --set "'.$value.'"';
98116
}
99117

100118
/**
@@ -112,7 +130,7 @@ private function setPlatform($platform)
112130
*
113131
* @param string $platform
114132
*/
115-
private function setnscvar($platform)
133+
public function setnscvar($platform)
116134
{
117135
switch ($platform) {
118136
case 'windows':
@@ -135,7 +153,7 @@ function cfgInit()
135153
{
136154
switch ($this->platform) {
137155
case 'windows':
138-
$this->nscBatArray = ['
156+
$this->nscConfArray = ['
139157
@ECHO OFF
140158
powershell Set-ExecutionPolicy ByPass
141159
set NSCDIR=%ProgramFiles%\NSClient++\
@@ -146,15 +164,15 @@ function cfgInit()
146164
rename "%NSCDIR%\nsclient.ini" nsclient.old
147165
'];
148166

149-
$this->nscBatArray[] = "\n".'SET ICIEDIT_HTML="%NSCDIR%/icinga-editor.htm"';
150-
$this->nscBatArray[] = "\n".'echo ^<html^> > %ICIEDIT_HTML%';
151-
$this->nscBatArray[] = "\n".'echo ^<head^>^<meta charset="UTF-8"^>^</head^> >> %ICIEDIT_HTML%';
152-
$this->nscBatArray[] = "\n".'echo ^<body^> >> %ICIEDIT_HTML%
167+
$this->nscConfArray[] = "\n".'SET ICIEDIT_HTML="%NSCDIR%/icinga-editor.htm"';
168+
$this->nscConfArray[] = "\n".'echo ^<html^> > %ICIEDIT_HTML%';
169+
$this->nscConfArray[] = "\n".'echo ^<head^>^<meta charset="UTF-8"^>^</head^> >> %ICIEDIT_HTML%';
170+
$this->nscConfArray[] = "\n".'echo ^<body^> >> %ICIEDIT_HTML%
153171
';
154172

155173
break;
156174
case 'linux':
157-
$this->nscBatArray = ['
175+
$this->nscConfArray = ['
158176
export NSCLIENT=`which nscp`
159177
export ICINGA_SERVER="'.$this->prefs['serverip'].'"
160178
'.$this->nscvar.' service --stop
@@ -172,7 +190,7 @@ function cfgInit()
172190
'];
173191
break;
174192
}
175-
$this->nscBatArray[] = $this->nscvar.' settings --generate';
193+
$this->nscConfArray[] = $this->nscvar.' settings --generate';
176194
}
177195

178196
/**
@@ -302,6 +320,7 @@ function cfgServices()
302320
$script_id = $command->getDataValue('script_id');
303321
if ($script_id) {
304322
$this->scriptsToDeploy[$command->getName()] = $script_id;
323+
$this->scriptsToDeployNames[$script_id] = $command->getDataValue('command_line');
305324
}
306325
$commandsCache[$remote] = $command->getData();
307326
}
@@ -342,10 +361,10 @@ function cfgServices()
342361
$serviceParams = $service['check_command-params'];
343362
switch ($this->platform) {
344363
case 'windows':
345-
$this->nscBatArray[] = "\n\nREM #".$service['service_id'].' '.$serviceName."\n";
364+
$this->nscConfArray[] = "\n\n".$this->comment." #".$service['service_id'].' '.$serviceName."\n";
346365
break;
347366
case 'linux':
348-
$this->nscBatArray[] = "\n\n# #".$service['service_id'].' '.$serviceName."\n";
367+
$this->nscConfArray[] = "\n\n# #".$service['service_id'].' '.$serviceName."\n";
349368
break;
350369
default:
351370
break;
@@ -396,39 +415,39 @@ public function cfgEnding()
396415
}
397416
switch ($this->platform) {
398417
case 'windows':
399-
$this->nscBatArray[] = "\n".'echo ^<h1^>'._('Konfigurace hosta').' '.$this->host->getName().'^</h1^> >> %ICIEDIT_HTML%';
400-
$this->nscBatArray[] = "\n".'echo ^<br^>^<a data-role="editor" href="'.Engine\Configurator::getBaseURL().'host.php?host_id='.$this->host->getId().'"^>'._('Host Configuration').'^</a^> >> %ICIEDIT_HTML%';
401-
$this->nscBatArray[] = "\n".'echo ^<br^>^<a data-role="bat" href="'.Engine\Configurator::getBaseURL().'nscpcfggen.php?host_id='.$this->host->getId().'"^>'._('Refresh sensor installation').' '.$this->host->getName().'_nscp.bat'.'^</a^> >> %ICIEDIT_HTML%';
418+
$this->nscConfArray[] = "\n".'echo ^<h1^>'._('Konfigurace hosta').' '.$this->host->getName().'^</h1^> >> %ICIEDIT_HTML%';
419+
$this->nscConfArray[] = "\n".'echo ^<br^>^<a data-role="editor" href="'.Engine\Configurator::getBaseURL().'host.php?host_id='.$this->host->getId().'"^>'._('Host Configuration').'^</a^> >> %ICIEDIT_HTML%';
420+
$this->nscConfArray[] = "\n".'echo ^<br^>^<a data-role="bat" href="'.Engine\Configurator::getBaseURL().'nscpcfggen.php?host_id='.$this->host->getId().'"^>'._('Refresh sensor installation').' '.$this->host->getName().'_nscp.bat'.'^</a^> >> %ICIEDIT_HTML%';
402421
if ($this->host->getDataValue('host_is_server') == 0) {
403422
$dtUrl = Engine\Configurator::getBaseURL().'downtime.php?host_id='.$this->host->getId();
404-
$this->nscBatArray[] = "\n".'echo ^<br^>^<a data-role="shutdown" href="'.$dtUrl.'&state=start"^>'._('Start host downtime').'^</a^> >> %ICIEDIT_HTML%';
405-
$this->nscBatArray[] = "\n".'echo ^<br^>^<a data-role="poweron" href="'.$dtUrl.'&state=stop"^>'._('End host downtime').'^</a^> >> %ICIEDIT_HTML%';
423+
$this->nscConfArray[] = "\n".'echo ^<br^>^<a data-role="shutdown" href="'.$dtUrl.'&state=start"^>'._('Start host downtime').'^</a^> >> %ICIEDIT_HTML%';
424+
$this->nscConfArray[] = "\n".'echo ^<br^>^<a data-role="poweron" href="'.$dtUrl.'&state=stop"^>'._('End host downtime').'^</a^> >> %ICIEDIT_HTML%';
406425
}
407-
$this->nscBatArray[] = "\n".'echo ^<br^>^<a data-role="confirm" href="'.$this->getCfgConfirmUrl().'"^>'._('Potvrzení konfigurace').'^</a^> >> %ICIEDIT_HTML%';
408-
$this->nscBatArray[] = "\n".'echo ^</body^> >> %ICIEDIT_HTML%';
409-
$this->nscBatArray[] = "\n".'echo ^</html^> >> %ICIEDIT_HTML%
426+
$this->nscConfArray[] = "\n".'echo ^<br^>^<a data-role="confirm" href="'.$this->getCfgConfirmUrl().'"^>'._('Potvrzení konfigurace').'^</a^> >> %ICIEDIT_HTML%';
427+
$this->nscConfArray[] = "\n".'echo ^</body^> >> %ICIEDIT_HTML%';
428+
$this->nscConfArray[] = "\n".'echo ^</html^> >> %ICIEDIT_HTML%
410429
';
411430
if ($this->host->getDataValue('host_is_server') == 0) {
412431
$upfile = 'C:\\Windows\\System32\\GroupPolicy\\Machine\\Scripts\\Startup\\hostup.ps1';
413-
$this->nscBatArray[] = "\n".'echo (new-object System.Net.WebClient).DownloadFile("'.$dtUrl.'&state=stop","C:\tmp\hostup.txt") > '.$upfile;
432+
$this->nscConfArray[] = "\n".'echo (new-object System.Net.WebClient).DownloadFile("'.$dtUrl.'&state=stop","C:\tmp\hostup.txt") > '.$upfile;
414433

415434
$downfile = 'C:\\Windows\\System32\\GroupPolicy\\Machine\\Scripts\\Shutdown\\hostdown.ps1';
416-
$this->nscBatArray[] = "\n".'echo (new-object System.Net.WebClient).DownloadFile("'.$dtUrl.'&state=start","C:\tmp\hostdown.txt") > '.$downfile;
435+
$this->nscConfArray[] = "\n".'echo (new-object System.Net.WebClient).DownloadFile("'.$dtUrl.'&state=start","C:\tmp\hostdown.txt") > '.$downfile;
417436
}
418437

419-
$this->nscBatArray[] = "\n".'
438+
$this->nscConfArray[] = "\n".'
420439
'.$this->nscvar.' service --start
421440
start "" %ICIEDIT_HTML%
422441
';
423442
break;
424443
case 'linux':
425-
$this->nscBatArray[] = "\n".'
444+
$this->nscConfArray[] = "\n".'
426445
curl "'.$this->getCfgConfirmUrl().'"
427446
service nscp start
428447
';
429448
break;
430449
default:
431-
$this->nscBatArray[] = $this->nscBatArray[] = "\n".'
450+
$this->nscConfArray[] = $this->nscConfArray[] = "\n".'
432451
';
433452
break;
434453
}
@@ -442,7 +461,7 @@ public function cfgEnding()
442461
*/
443462
public function getCfg($send = TRUE)
444463
{
445-
$nscbat = implode('', $this->nscBatArray);
464+
$nscbat = implode('', $this->nscConfArray);
446465
if ($send) {
447466
header('Content-Description: File Transfer');
448467
header('Content-Type: application/octet-stream');
@@ -454,7 +473,7 @@ public function getCfg($send = TRUE)
454473
switch ($this->platform) {
455474
case 'windows':
456475
if ($send) {
457-
header('Content-Disposition: attachment; filename='.$this->host->getName().'_nscp.bat');
476+
header('Content-Disposition: attachment; filename='.$this->host->getName().'_nscp.'.$this->formatSuffix);
458477
}
459478
$nscbat = str_replace("\n", "\r\n", $nscbat);
460479
break;
@@ -474,6 +493,11 @@ public function getCfg($send = TRUE)
474493
}
475494
}
476495

496+
/**
497+
* Where To confirm sensor status ?
498+
*
499+
* @return string
500+
*/
477501
function getCfgConfirmUrl()
478502
{
479503
return Engine\Configurator::getBaseURL().'cfgconfirm.php?hash='.$this->host->getConfigHash().'&host_id='.$this->host->getId();
@@ -487,7 +511,7 @@ public function deployScripts()
487511
if (count($this->scriptsToDeploy)) {
488512
switch ($this->platform) {
489513
case 'windows':
490-
$this->nscBatArray[] = "\n".'echo ^<h2^>'._('Skripty').'^</h2^> >> %ICIEDIT_HTML%
514+
$this->nscConfArray[] = "\n".'echo ^<h2^>'._('Skripty').'^</h2^> >> %ICIEDIT_HTML%
491515
';
492516
break;
493517
}
@@ -496,17 +520,17 @@ public function deployScripts()
496520
foreach ($this->scriptsToDeploy as $script_name => $script_id) {
497521
switch ($this->platform) {
498522
case 'windows':
499-
$this->nscBatArray[] = "\n".'echo ^<a data-role="script" href="'.Engine\Configurator::getBaseURL().'scriptget.php?script_id='.$script_id.'"^>'.$script_name.'^</a^>^<br^> >> %ICIEDIT_HTML%
523+
$this->nscConfArray[] = "\n".'echo ^<a data-role="script" href="'.Engine\Configurator::getBaseURL().'scriptget.php?script_id='.$script_id.'"^>'.$script_name.'^</a^>^<br^> >> %ICIEDIT_HTML%
500524
';
501525
break;
502526
case 'linux':
503-
$this->nscBatArray[] = "\n".'
527+
$this->nscConfArray[] = "\n".'
504528
# '.$script_name.'
505529
curl "'.Engine\Configurator::getBaseURL().'scriptget.php?script_id='.$script_id.'"
506530
';
507531
break;
508532
default:
509-
$this->nscBatArray[] = $this->nscBatArray[] = "\n".'
533+
$this->nscConfArray[] = $this->nscConfArray[] = "\n".'
510534
'.$this->nscvar.' test
511535
';
512536
break;

0 commit comments

Comments
 (0)