forked from nconf/nconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone_host_write2db.php
339 lines (278 loc) · 15.7 KB
/
clone_host_write2db.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
require_once 'include/head.php';
if ( isset($_SESSION["cache"]["clone"]) ) unset($_SESSION["cache"]["clone"]);
if (DB_NO_WRITES == 1) {
message($info, "DB_NO_WRITES = 1: No DB inserts or modifications will be performed");
}
#############
# Check for existing entry
$query = 'SELECT fk_id_item,attr_value FROM ConfigValues,ConfigAttrs,ConfigClasses
WHERE id_attr=fk_id_attr
AND attr_name="host_name"
AND id_class=fk_id_class
AND config_class="host"
AND attr_value = "'.$_POST["hostname"].'"';
$result = mysql_query($query);
#############
# Entry exists ?
if (mysql_num_rows($result)){
echo 'Entry with name "'.$_POST["hostname"].'" already exists!';
echo '<br><br>Click for details: ';
while($entry = mysql_fetch_assoc($result)){
echo '<a href="detail.php?id='.$entry["fk_id_item"].'">'.$entry["attr_value"].'</a>';
}
echo '<br><br>or go <a href="javascript:history.go(-1)">back</a>';
}else{
# Entry not existing, lets try to clone...
echo "<h2> Clone host</h2>";
?>
<table>
<tr>
<td>
<?php
#############
# check mandatory entries
#$write2db = "no";
$arr_mandatory = array("template_id","hostname", "ip");
$write2db = "yes";
foreach ($arr_mandatory as $mandatory){
if ( ( isset($_POST[$mandatory]) ) AND ( $_POST[$mandatory] != "") ){
message($debug, "$mandatory: ok");
}else{
message($error, "$mandatory: mandatory field");
message($info, SELECT_EMPTY_FIELD, "overwrite");
$write2db = "no";
}
}
#############
# write to DB
if ($write2db == "yes"){
# generate new item_id
$query = 'INSERT INTO ConfigItems (id_item,fk_id_class) VALUES (NULL,(SELECT id_class FROM ConfigClasses WHERE config_class="host"))';
$new_host_id = db_handler($query, "insert_id", "insert new host");
if ($result){
history_add("created", "host", $_POST["hostname"], $new_host_id);
message ($debug, 'Successfully added new host');
}else{
message ($error, 'Error while adding new host');
}
#############
# insert data deferring from template
$hostname_id = db_templates("host_attr_id", "host_name");
$alias_id = db_templates("host_attr_id", "alias"); // the alias (in host class)means "FQDN" as friendlyname
$ip_id = db_templates("host_attr_id", "address");
$query = 'INSERT INTO ConfigValues (attr_value,fk_id_item,fk_id_attr)
VALUES ("'.$_POST["hostname"].'",'.$new_host_id.', '.$hostname_id.'),
("'.$_POST["alias"].'",'.$new_host_id.', '.$alias_id.'),
("'.$_POST["ip"].'",'.$new_host_id.', '.$ip_id.')';
$result = db_handler($query, "result", "insert data deferring from template");
if ($result){
# add to history
history_add("added", $hostname_id, $_POST["hostname"], $new_host_id);
history_add("added", $alias_id, $_POST["alias"], $new_host_id);
history_add("added", $ip_id, $_POST["ip"], $new_host_id);
}
#############
# clone basic data
NConf_DEBUG::open_group("clone basic data");
$query = 'INSERT INTO ConfigValues (fk_id_attr,attr_value,fk_id_item)
SELECT id_attr,attr_value,'.$new_host_id.' FROM ConfigAttrs,ConfigValues,ConfigItems
WHERE id_attr=fk_id_attr
AND id_item=fk_id_item
AND (attr_name <> "host_name" AND attr_name <> "alias" AND attr_name <> "address")
AND id_item='.$_POST["template_id"].'
ORDER BY ordering';
$result = db_handler($query, "result", "clone basic data");
if ($result){
# enter basic data to history
$query = 'SELECT id_attr,attr_value,'.$new_host_id.' FROM ConfigAttrs,ConfigValues,ConfigItems
WHERE id_attr=fk_id_attr
AND id_item=fk_id_item
AND (attr_name <> "host_name" AND attr_name <> "alias" AND attr_name <> "address")
AND id_item='.$_POST["template_id"].'
ORDER BY ordering';
$b_data = db_handler($query, "array", "get basic data");
foreach($b_data as $data){
history_add("added", $data["id_attr"], $data["attr_value"], $data["$new_host_id"]);
}
}
#############
# clone items linked as child (except services) e.g. other hosts
NConf_DEBUG::open_group("clone items linked as child");
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr,cust_order)
SELECT fk_id_item,'.$new_host_id.',fk_id_attr,cust_order FROM ItemLinks,ConfigItems,ConfigClasses
WHERE id_item = fk_id_item
AND id_class = fk_id_class
AND config_class <> "service"
AND fk_item_linked2 = '.$_POST["template_id"].' ORDER BY fk_id_item';
$result = db_handler($query, "result", "cloned data linked as child");
if ($result){
# enter linked CHILD items to history
$query = 'SELECT fk_id_item,'.$new_host_id.',fk_id_attr FROM ItemLinks,ConfigItems,ConfigClasses
WHERE id_item = fk_id_item
AND id_class = fk_id_class
AND config_class <> "service"
AND fk_item_linked2 = '.$_POST["template_id"].' ORDER BY fk_id_attr,cust_order';
$l_child_data = db_handler($query, "array", "get linked CHILD items for history");
foreach($l_child_data as $data){
# ! Attention because the entry is a CHILD, the assigment has to be logged in the history of the parent entry !!!
# so "$new_host_id" and "fk_id_item" is simply switched
history_add("assigned", $data["fk_id_attr"], $data["$new_host_id"], $data["fk_id_item"], "resolve_assignment");
}
}
#############
# insert deferring parent hosts
if( ( isset($_POST["parents"]) ) &&
( ($_POST["parents"][0] != "CLONE-PARENTS") AND ($_POST["parents"][0] != "") )
){
# Fetch attr_id from attribute "parents"
$attr_parents_id = db_templates("host_attr_id", "parents");
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr) VALUES ';
$counter = 0;
foreach ($_POST["parents"] as $parent){
if($counter != 0){
$query .= ",";
}
$query .= '('.$new_host_id.','.$parent.','.$attr_parents_id.')';
$counter++;
history_add("assigned", $attr_parents_id, $parent, $new_host_id, "resolve_assignment");
}
$result = db_handler($query, "insert", "insert data deferring parent hosts");
}
#############
# Clone linked data / parents
NConf_DEBUG::open_group("clone linked data");
if( (isset($_POST["parents"][0])) AND ($_POST["parents"][0] == "CLONE-PARENTS") ){
# clone all items linked
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr,cust_order)
SELECT '.$new_host_id.',fk_item_linked2,fk_id_attr,cust_order
FROM ItemLinks WHERE fk_id_item = '.$_POST["template_id"].'
ORDER BY fk_item_linked2';
$history_query = 'SELECT '.$new_host_id.',fk_item_linked2,fk_id_attr
FROM ItemLinks WHERE fk_id_item = '.$_POST["template_id"].'
ORDER BY fk_item_linked2';
}else{
# clone items linked (except for parent hosts)
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr,cust_order)
SELECT '.$new_host_id.',fk_item_linked2,fk_id_attr,cust_order
FROM ItemLinks WHERE fk_id_item = '.$_POST["template_id"].'
AND ((SELECT attr_name FROM ConfigAttrs WHERE id_attr=fk_id_attr) <> "parents"
AND (SELECT config_class FROM ConfigItems,ConfigClasses WHERE id_item=fk_item_linked2 AND id_class=fk_id_class) <> "host")
ORDER BY fk_item_linked2';
$history_query = 'SELECT '.$new_host_id.',fk_item_linked2,fk_id_attr
FROM ItemLinks WHERE fk_id_item = '.$_POST["template_id"].'
AND ((SELECT attr_name FROM ConfigAttrs WHERE id_attr=fk_id_attr) <> "parents"
AND (SELECT config_class FROM ConfigItems,ConfigClasses WHERE id_item=fk_item_linked2 AND id_class=fk_id_class) <> "host")
ORDER BY fk_id_attr,cust_order';
}
$result = db_handler($query, "result", "cloned linked data");
if ($result){
# enter linked items to history
$l_data = db_handler($history_query, "array", "get linked items for history");
foreach($l_data as $data){
history_add("assigned", $data["fk_id_attr"], $data["fk_item_linked2"], $data["$new_host_id"], "resolve_assignment");
}
}
#############
# clone each service individually
# fetch list of services
$query = 'SELECT fk_id_item,fk_id_attr FROM ItemLinks,ConfigItems,ConfigClasses
WHERE id_item = fk_id_item
AND id_class = fk_id_class
AND config_class = "service"
AND fk_item_linked2 = '.$_POST["template_id"].' ORDER BY fk_id_item';
$services = db_handler($query, "array", "fetch list of services");
# iterate through all services
foreach ($services as $service_tpl){
NConf_DEBUG::open_group("clone service ".$service_tpl["fk_id_item"]);
# create a new item_id for each cloned service
$query = 'INSERT INTO ConfigItems (id_item,fk_id_class) VALUES (NULL,(SELECT id_class FROM ConfigClasses WHERE config_class="service"))';
$new_service_id = db_handler($query, "insert_id", "create a new item_id for each cloned service");
# link new service to cloned host
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr)
VALUES ('.$new_service_id.','.$new_host_id.','.$service_tpl["fk_id_attr"].')';
$result = db_handler($query, "insert", "link new service to cloned host");
if ($result){
message ($debug, 'Successfully linked service '.$new_service_id.' to host '.$new_host_id);
}else{
message ($error, 'Error linking service '.$new_service_id.' to host '.$new_host_id.' '.$query);
}
# clone basic data of original service onto new service
$query = 'INSERT INTO ConfigValues (fk_id_attr,attr_value,fk_id_item)
SELECT id_attr,attr_value,'.$new_service_id.' FROM ConfigAttrs,ConfigValues,ConfigItems
WHERE id_attr=fk_id_attr
AND id_item=fk_id_item
AND id_item='.$service_tpl["fk_id_item"].'
ORDER BY ordering';
$result = db_handler($query, "insert", "clone basic data of original service onto new service");
if ($result){
# service added
$service_name = db_templates("naming_attr", $service_tpl["fk_id_item"]);
$service_name_attr_id = db_templates("get_attr_id", "service", "service_description");
# created entry
history_add("created", "service", $service_name, $new_service_id);
history_add("added", $service_name_attr_id, $service_name, $new_service_id);
history_add("added", "service", $new_service_id, $new_host_id, "resolve_assignment");
# HISTORY add :basic data
$query = 'SELECT id_attr,attr_value FROM ConfigAttrs,ConfigValues,ConfigItems
WHERE id_attr=fk_id_attr
AND id_item=fk_id_item
AND id_item='.$service_tpl["fk_id_item"].'
AND naming_attr = "no"
ORDER BY ordering';
$basic_entries = db_handler($query, "array", "for history: get basic data");
foreach ($basic_entries AS $entry){
history_add("added", $entry["id_attr"], $entry["attr_value"], $new_service_id);
}
}
# clone items linked to original service onto new service
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr,cust_order)
SELECT '.$new_service_id.',fk_item_linked2,fk_id_attr,cust_order FROM ItemLinks
WHERE fk_id_item='.$service_tpl["fk_id_item"].'
AND fk_item_linked2 <> '.$_POST["template_id"].'
ORDER BY fk_item_linked2';
$result = db_handler($query, "insert", "clone items linked to original service onto new service");
# HISTORY add :linked items
$query = 'SELECT fk_item_linked2,fk_id_attr,cust_order FROM ItemLinks
WHERE fk_id_item='.$service_tpl["fk_id_item"].'
AND fk_item_linked2 <> '.$_POST["template_id"].'
ORDER BY fk_item_linked2';
$linked_items = db_handler($query, "array", "for history: get linked items");
foreach ($linked_items AS $entry){
history_add("assigned", $entry["fk_id_attr"], $entry["fk_item_linked2"], $new_service_id, "resolve_assignment");
}
# clone items linked as child of original service onto new service
$query = 'INSERT INTO ItemLinks (fk_id_item,fk_item_linked2,fk_id_attr,cust_order)
SELECT fk_id_item,'.$new_service_id.',fk_id_attr,cust_order FROM ItemLinks
WHERE fk_item_linked2 = '.$service_tpl["fk_id_item"].' ORDER BY fk_id_item';
$result = db_handler($query, "insert", "clone items linked as child of original service onto new service");
# HISTORY add :linked as child items
$query = 'SELECT fk_id_item,fk_id_attr,cust_order FROM ItemLinks
WHERE fk_item_linked2 = '.$service_tpl["fk_id_item"].' ORDER BY fk_id_item';
$linkedAsChild_items = db_handler($query, "array", "for history: get linked as child items");
foreach ($linkedAsChild_items AS $entry){
history_add("assigned", $entry["fk_id_attr"], $new_service_id, $entry["fk_id_item"], "resolve_assignment");
}
} //foreach
#############
# show success message
echo '<b>Successfully cloned selected host to "'.$_POST["hostname"].'"</b>';
echo '<br><br>Click for details: ';
echo '<a href="detail.php?id='.$new_host_id.'">'.$_POST["hostname"].'</a>';
}else{ # else of write2db
// Cache
foreach ($_POST as $key => $value) {
$_SESSION["cache"]["clone"][$key] = $value;
}
// Error message
echo NConf_DEBUG::show_debug('ERROR', TRUE, $_SESSION["go_back_page"]);
}# end of write2db
?>
</td>
</tr>
</table>
<?php
} // END Entry exists ?
mysql_close($dbh);
require_once 'include/foot.php';
?>