-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd_host.php
200 lines (182 loc) · 6.2 KB
/
add_host.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
<?php
/*
Lilac - A Nagios Configuration Tool
Copyright (C) 2018 EyesOfNetwork Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
Filename: add_host.php
*/
include_once('includes/config.inc');
session_start(); //Add a session for the template thing
if(isset($_POST['request']) && $_POST['request'] == 'add_host') {
// Check for pre-existing host template with same name
if($lilac->host_exists($_POST['host_manage']['host_name'])) {
$error = "A host with that name already exists!";
}
else {
// Field Error Checking
if(count($_POST['host_manage'])) {
foreach($_POST['host_manage'] as $tempVariable)
$tempVariable = trim($tempVariable);
}
if($_POST['host_manage']['host_name'] == '' || $_POST['host_manage']['alias'] == '' || $_POST['host_manage']['address'] == '') {
$error = "Fields shown are required and cannot be left blank.";
}
else {
// All is well for error checking, add the host into the db.
$tempHost = new NagiosHost();
$tempHost->setName(preg_replace('/[^a-zA-Z0-9_ -]/s','', $_POST['host_manage']['host_name']));
$tempHost->setAlias($_POST['host_manage']['alias']);
if(isset($_GET['parent_id'])) {
// Get the host by that parent_id
$host = NagiosHostPeer::retrieveByPk($_GET['parent_id']);
if($host) {
// valid host, add parent
$tempHost->addParentByName($host->getName());
}
}
$tempHost->setAddress($_POST['host_manage']['address']);
if(isset($_POST['host_manage']['display_name'])) {
$tempVariable = trim($_POST['host_manage']['display_name']);
if ( ! empty($tempVariable) ) {
$tempHost->setDisplayName($tempVariable);
} else {
$tempHost->setDisplayName(null);
}
} else {
$tempHost->setDisplayName(null);
}
$tempHost->save();
//Host is save, now attach the template to it.
foreach($_SESSION['templates'] as $pk){
$template = NagiosHostTemplatePeer::retrieveByPK($pk);
if(!$template) {
$error = "That host template is not found.";
}
else {
// We need to get the count of templates already inherited
$templateList = $tempHost->getNagiosHostTemplateInheritances();
foreach($templateList as $tempTemplate) {
if($tempTemplate->getId() == $pk) {
$error = "That template already exists in the inheritance chain.";
}
}
if(empty($error)) {
$newInheritance = new NagiosHostTemplateInheritance();
$newInheritance->setNagiosHost($tempHost);
$newInheritance->setNagiosHostTemplateRelatedByTargetTemplate($template);
$newInheritance->setOrder(count($templateList));
try {
$newInheritance->save();
$success = "Template added to inheritance chain.";
}
catch(Exception $e) {
$error = $e->getMessage();
}
}
}
}
unset($_SESSION['templates']); //Kill the Template we just save.
header("Location: hosts.php?id=" . $tempHost->getId());
die();
}
}
}
unset($_SESSION['templates']); //Kill any existing Template
$add_template_list[] = array("host_template_id" => '', "template_name" => "None");
$lilac->get_host_template_list( $template_list);
if(count($template_list)) {
foreach($template_list as $tempTemplate) {
$add_template_list[] = array('host_template_id' => $tempTemplate->getId(), 'template_name' => $tempTemplate->getName());
}
}
print_header("Add New Host");
$title = "Add A Top-Level Host";
if(isset($_GET['parent_id'])) {
$tempHostInfo = NagiosHostPeer::retrieveByPK($_GET['parent_id']);
if($tempHostInfo) {
$title = "Add A Host Under " . $tempHostInfo->getName();
}
}
print_window_header($title, "100%");
?>
<form name="host_add_form" method="post" action="add_host.php<?php if(isset($_GET['parent_id'])) print("?parent_id=" . $_GET['parent_id']);?>">
<input type="hidden" name="request" value="add_host" />
<?php
if(isset($_GET['parent_id']) && $_GET['parent_id'] != 0) {
?>
<input type="hidden" name="host_manage[parents]" value="<?php echo $_GET['parent_id'];?>">
<?php
}
?>
<?php double_pane_form_window_start(); ?>
<tr bgcolor="f0f0f0">
<td colspan="2" class="formcell">
<b>Host Name:</b><br />
<input type="text" size="40" name="host_manage[host_name]" value="">
<?php echo $lilac->element_desc("host_name", "nagios_hosts_desc"); ?><br />
<br />
</td>
</tr>
<tr bgcolor="eeeeee">
<td colspan="2" class="formcell">
<b>Host Description:</b><br />
<input type="text" size="40" name="host_manage[alias]" value="">
<?php echo $lilac->element_desc("alias", "nagios_hosts_desc"); ?><br />
<br />
</td>
</tr>
<tr bgcolor="f0f0f0">
<td colspan="2" class="formcell">
<b>Address:</b><br />
<input type="text" size="40" name="host_manage[address]" value="">
<?php echo $lilac->element_desc("address", "nagios_hosts_desc"); ?><br />
<br />
</td>
</tr>
<tr bgcolor="f0f0f0">
<td colspan="2" class="formcell">
<b>Display Name (Optional):</b><br />
<input type="text" size="40" name="host_manage[display_name]" value="">
<?php echo $lilac->element_desc("display_name", "nagios_hosts_desc"); ?><br />
<br />
</td>
</tr>
<tr bgcolor="f0f0f0">
<span id="output"></span>
</tr>
<?php double_pane_form_window_finish(); ?>
<input class="btn btn-primary" type="submit" value="Add Host" /> <a class="btn btn-default" href="hosts.php">Cancel</a>
<br /><br />
</form>
<script type="text/javascript">
function appel(id,option) {
params = "id="+id+"&option="+option;
$.post("add_host_ajax.php", params, result, "html");
}
function result(datas){
$("#output").html(datas);
}
function getID() {
appel($("select[name='<?php echo 'hostmanage[template_add][template_id]';?>']")[0].value);
}
appel(null);
</script>
<?php
print_window_footer();
?>
<br />
<?php
print_footer();
?>