ona  18.1.1
About: OpenNetAdmin provides a database managed inventory of your IP network (with Web and CLI interface).
  Fossies Dox: ona-18.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

host.inc.php
Go to the documentation of this file.
1 <?php
2 // DON'T put whitespace at the beginning or end of this file!!!
3 
4 
5 
6 
7 
9 // Function: host_add (string $options='')
10 //
11 // Input Options:
12 // $options = key=value pairs of options for this function.
13 // multiple sets of key=value pairs should be separated
14 // by an "&" symbol.
15 //
16 // Output:
17 // Returns a two part list:
18 // 1. The exit status of the function. 0 on success, non-zero on
19 // error. All errors messages are stored in $self['error'].
20 // 2. A textual message for display on the console or web interface.
21 //
22 // Example: list($status, $result) = host_add('host=test&type=something');
24 function host_add($options="") {
25  global $conf, $self, $onadb;
26 
27  // Version - UPDATE on every edit!
28  $version = '1.12';
29 
30  printmsg("DEBUG => host_add({$options}) called", 3);
31 
32  // Parse incoming options string to an array
33  $options = parse_options($options);
34 
35  // Return the usage summary if we need to
36  if ($options['help'] or !($options['host'] and $options['type'] and $options['ip']) ) {
37  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
38  $self['error'] = 'ERROR => Insufficient parameters';
39  return(array(1,
40 <<<EOM
41 
43 Add a new host
44 
45  Synopsis: host_add [KEY=VALUE] ...
46 
47  Required:
48  host=NAME[.DOMAIN] Hostname for new DNS record
49  type=TYPE or ID Device/model type or ID
50  ip=ADDRESS IP address (numeric or dotted)
51 
52  Optional:
53  notes=NOTES Textual notes
54  location=REF Reference of location
55  device=NAME|ID The device this host is associated with
56 
57  Optional, add an interface too:
58  mac=ADDRESS Mac address (most formats are ok)
59  name=NAME Interface name (i.e. "FastEthernet0/1.100")
60  description=TEXT Brief description of the interface
61  addptr=Y|N Auto add a PTR record for new host/IP (default: Y)
62 
63 
64 EOM
65  ));
66  }
67 
68  // Sanitize addptr.. set it to Y if it is not set
69  $options['addptr'] = sanitize_YN($options['addptr'], 'Y');
70 
71  // clean up what is passed in
72  $options['ip'] = trim($options['ip']);
73  $options['mac'] = trim($options['mac']);
74  $options['name'] = trim($options['name']);
75  $options['host'] = trim($options['host']);
76 
77  // Validate that there isn't already another interface with the same IP address
78  list($status, $rows, $interface) = ona_get_interface_record(array('ip_addr' => $options['ip']));
79  if ($rows) {
80  printmsg("DEBUG => host_add() IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") is already in use!",3);
81  $self['error'] = "ERROR => host_add() IP conflict: That IP address (" . ip_mangle($orig_ip,'dotted') . ") is already in use!";
82  return(array(4, $self['error'] . "\n" .
83  "INFO => Conflicting interface record ID: {$interface['id']}\n"));
84  }
85 
86  // Find the Location ID to use
87  if ($options['location']) {
88  list($status, $rows, $loc) = ona_find_location($options['location']);
89  if ($status or !$rows) {
90  printmsg("DEBUG => The location specified, {$options['location']}, does not exist!", 3);
91  $self['error'] = "ERROR => The location specified, {$options['location']}, does not exist!";
92  return(array(2, "{$self['error']}\n"));
93  }
94  printmsg("DEBUG => Location selected: {$loc['reference']}, location name: {$loc['name']}", 3);
95  } else {
96  $loc['id'] = 0;
97  }
98 
99  // Find the Device Type ID (i.e. Type) to use
100  list($status, $rows, $device_type) = ona_find_device_type($options['type']);
101  if ($status or $rows != 1 or !$device_type['id']) {
102  printmsg("DEBUG => The device type specified, {$options['type']}, does not exist!", 3);
103  return(array(3, "ERROR => The device type specified, {$options['type']}, does not exist!\n"));
104  }
105  printmsg("DEBUG => Device type selected: {$device_type['model_description']} Device ID: {$device_type['id']}", 3);
106 
107 
108  // Sanitize "security_level" option
109  $options['security_level'] = sanitize_security_level($options['security_level']);
110  if ($options['security_level'] == -1) {
111  printmsg("DEBUG => Sanitize security level failed either ({$options['security_level']}) is invalid or is higher than user's level!", 3);
112  return(array(3, $self['error'] . "\n"));
113  }
114 
115 
116  // Determine the real hostname to be used --
117  // i.e. add .something.com, or find the part of the name provided
118  // that will be used as the "domain". This means testing many
119  // domain names against the DB to see what's valid.
120  //
121  // Find the domain name piece of $search.
122  // If we are specifically passing in a domain, use its value. If we dont have a domain
123  // then try to find it in the name that we are setting.
124  if($options['domain']) {
125  // Find the domain name piece of $search
126  list($status, $rows, $domain) = ona_find_domain($options['domain'],0);
127  } else {
128  list($status, $rows, $domain) = ona_find_domain($options['host'],0);
129  }
130  if (!isset($domain['id'])) {
131  printmsg("ERROR => Unable to determine domain name portion of ({$options['host']})!", 3);
132  $self['error'] = "ERROR => Unable to determine domain name portion of ({$options['host']})!";
133  return(array(3, $self['error'] . "\n"));
134  }
135 
136  printmsg("DEBUG => ona_find_domain({$options['host']}) returned: {$domain['fqdn']}", 3);
137 
138  // Now find what the host part of $search is
139  $hostname = str_replace(".{$domain['fqdn']}", '', $options['host']);
140 
141  // Validate that the DNS name has only valid characters in it
142  $hostname = sanitize_hostname($hostname);
143  if (!$hostname) {
144  printmsg("ERROR => Invalid host name ({$options['host']})!", 3);
145  $self['error'] = "ERROR => Invalid host name ({$options['host']})!";
146  return(array(4, $self['error'] . "\n"));
147  }
148 
149 
150  // Debugging
151  printmsg("DEBUG => Using hostname: {$hostname} Domainname: {$domain['fqdn']}, Domain ID: {$domain['id']}", 3);
152 
153  // Validate that there isn't already any dns record named $host['name'] in the domain $host_domain_id.
154  $h_status = $h_rows = 0;
155  // does the domain $host_domain_id even exist?
156  list($d_status, $d_rows, $d_record) = ona_get_dns_record(array('name' => $hostname, 'domain_id' => $domain['id']));
157  if ($d_status or $d_rows) {
158  printmsg("DEBUG => The name {$hostname}.{$domain['fqdn']} is already in use, the primary name for a host should be unique!",3);
159  $self['error'] = "ERROR => Another DNS record named {$hostname}.{$domain['fqdn']} is already in use, the primary name for a host should be unique!";
160  return(array(5, $self['error'] . "\n"));
161  }
162 
163  // Check permissions
164  if (!auth('host_add')) {
165  $self['error'] = "Permission denied!";
166  printmsg($self['error'], 0);
167  return(array(10, $self['error'] . "\n"));
168  }
169 
170  // Get the next ID for the new host record
171  $id = ona_get_next_id('hosts');
172  if (!$id) {
173  $self['error'] = "ERROR => The ona_get_next_id('hosts') call failed!";
174  printmsg($self['error'], 0);
175  return(array(7, $self['error'] . "\n"));
176  }
177  printmsg("DEBUG => ID for new host record: $id", 3);
178 
179  // Get the next ID for the new device record or use the one passed in the CLI
180  if (!$options['device']) {
181  $host['device_id'] = ona_get_next_id('devices');
182  if (!$id) {
183  $self['error'] = "ERROR => The ona_get_next_id('device') call failed!";
184  printmsg($self['error'], 0);
185  return(array(7, $self['error'] . "\n"));
186  }
187  printmsg("DEBUG => ID for new device record: $id", 3);
188  } else {
189  list($status, $rows, $devid) = ona_find_device($options['device']);
190  if (!$rows) {
191  printmsg("DEBUG => The device specified, {$options['device']}, does not exist!",3);
192  $self['error'] = "ERROR => The device specified, {$options['device']}, does not exist!";
193  return(array(7, $self['error'] . "\n"));
194  }
195  $host['device_id'] = $devid['id'];
196  }
197 
198 
199  // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
200  $options['notes'] = str_replace('\\=','=',$options['notes']);
201  $options['notes'] = str_replace('\\&','&',$options['notes']);
202 
203  // Add the device record
204  // FIXME: (MP) quick add of device record. more detail should be looked at here to ensure it is done right
205 // FIXME: MP this should use the run_module('device_add')!!! when it is ready
206  list($status, $rows) = db_insert_record(
207  $onadb,
208  'devices',
209  array(
210  'id' => $host['device_id'],
211  'device_type_id' => $device_type['id'],
212  'location_id' => $loc['id'],
213  'primary_host_id' => $id
214  // FIXME: (MP) add in the asset tag and serial number stuff too
215  )
216  );
217  if ($status or !$rows) {
218  $self['error'] = "ERROR => host_add() SQL Query failed adding device: " . $self['error'];
219  printmsg($self['error'], 0);
220  return(array(6, $self['error'] . "\n"));
221  }
222 
223  // Add the host record
224  // FIXME: (PK) Needs to insert to multiple tables for e.g. name and domain_id.
225  list($status, $rows) = db_insert_record(
226  $onadb,
227  'hosts',
228  array(
229  'id' => $id,
230  'parent_id' => 0, // set to 0 for now until feature is implemented TODO
231  'primary_dns_id' => 0, // Unknown at this point.. needs to be added afterwards
232  'device_id' => $host['device_id'],
233  'notes' => $options['notes']
234  )
235  );
236  if ($status or !$rows) {
237  $self['error'] = "ERROR => host_add() SQL Query failed adding host: " . $self['error'];
238  printmsg($self['error'], 0);
239  return(array(6, $self['error'] . "\n"));
240  }
241 
242  // Else start an output message
243  $text = "INFO => Host ADDED: {$hostname}.{$domain['fqdn']}";
244  printmsg($text,0);
245  $text .= "\n";
246 
247 
248  // We must always have an IP now to add an interface, call that module now:
249  // since we have no name yet, we need to use the ID of the new host as the host option for the following module calls
250  $options['host'] = $id;
251  // for annoying reasons we need to keep track of what was set first
252  $options['addptrsave'] = $options['addptr'];
253  // Interface adds can add PTR records, lets let the A record add that happens next add it instead.
254  $options['addptr'] = '0';
255 
256  printmsg("DEBUG => host_add() ({$hostname}.{$domain['fqdn']}) calling interface_add() ({$options['ip']})", 3);
257  list($status, $output) = run_module('interface_add', $options);
258  if ($status)
259  return(array($status, $output));
260  $text .= $output;
261 
262  // Find the interface_id for the interface we just added
263  list($status, $rows, $int) = ona_find_interface($options['ip']);
264 
265  // make the dns record type A
266  $options['type'] = 'A';
267  // FIXME: MP I had to force the name value here. name is comming in as the interface name. this is nasty!
268  $options['name'] = "{$hostname}.{$domain['fqdn']}";
269  $options['domain'] = $domain['fqdn'];
270  // And we will go ahead and auto add the ptr. the user can remove it later if they dont want it. FIXME: maybe create a checkbox on the host edit
271  $options['addptr'] = $options['addptrsave'];
272 
273  // Add the DNS entry with the IP address etc
274  printmsg("DEBUG => host_add() ({$hostname}.{$domain['fqdn']}) calling dns_record_add() ({$options['ip']})", 3);
275  list($status, $output) = run_module('dns_record_add', $options);
276  if ($status)
277  return(array($status, $output));
278  $text .= $output;
279 
280  // find the dns record we just added so we can use its ID as the primary_dns_id for the host.
281  list($status, $rows, $dnsrecord) = ona_get_dns_record(array('name' => $hostname, 'domain_id' => $domain['id'], 'interface_id' => $int['id'], 'type' => 'A'));
282 
283  // Set the primary_dns_id to the dns record that was just added
284  list($status, $rows) = db_update_record($onadb, 'hosts', array('id' => $id), array('primary_dns_id' => $dnsrecord['id']));
285  if ($status or !$rows) {
286  $self['error'] = "ERROR => host_add() SQL Query failed to update primary_dns_id for host: " . $self['error'];
287  printmsg($self['error'], 0);
288  return(array(8, $self['error'] . "\n"));
289  }
290 
291  return(array(0, $text));
292 
293 }
294 
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
306 // Function: host_modify (string $options='')
307 //
308 // Input Options:
309 // $options = key=value pairs of options for this function.
310 // multiple sets of key=value pairs should be separated
311 // by an "&" symbol.
312 //
313 // Output:
314 // Returns a two part list:
315 // 1. The exit status of the function. 0 on success, non-zero on
316 // error. All errors messages are stored in $self['error'].
317 // 2. A textual message for display on the console or web interface.
318 //
319 // Example: list($status, $result) = host_modify('FIXME: blah blah blah');
321 function host_modify($options="") {
322  global $conf, $self, $onadb;
323 
324  // Version - UPDATE on every edit!
325  $version = '1.07';
326 
327  printmsg("DEBUG => host_modify({$options}) called", 3);
328 
329  // Parse incoming options string to an array
330  $options = parse_options($options);
331 
332  // Return the usage summary if we need to
333  if ($options['help'] or
334  (!$options['interface'] and !$options['host']) or
335  (!$options['set_host'] and
336  !$options['set_type'] and
337  !$options['set_location'] and
338  !$options['set_notes']
339  ) ) {
340  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
341  $self['error'] = 'ERROR => Insufficient parameters';
342  return(array(1,
343 <<<EOM
344 
346 Modify a host record
347 
348  Synopsis: host_modify [KEY=VALUE] ...
349 
350  Where:
351  host=NAME[.DOMAIN] or ID Select host by hostname or ID
352  or
353  interface=[ID|IP|MAC] Select host by IP or MAC
354 
355  Update:
356  set_type=TYPE or ID Change device/model type or ID
357  set_notes=NOTES Change the textual notes
358  set_location=REF Reference for location
359  set_device=NAME|ID Name or ID of the device this host is associated with
360 
361 EOM
362  ));
363  }
364 
365  // clean up what is passed in
366  $options['interface'] = trim($options['interface']);
367  $options['host'] = trim($options['host']);
368 
369  //
370  // Find the host record we're modifying
371  //
372 
373  // If they provided a hostname / ID let's look it up
374  if ($options['host'])
375  list($status, $rows, $host) = ona_find_host($options['host']);
376 
377  // If they provided a interface ID, IP address, interface name, or MAC address
378  else if ($options['interface']) {
379  // Find an interface record by something in that interface's record
380  list($status, $rows, $interface) = ona_find_interface($options['interface']);
381  if ($status or !$rows) {
382  printmsg("DEBUG => Interface not found ({$options['interface']})!",3);
383  $self['error'] = "ERROR => Interface not found ({$options['interface']})!";
384  return(array(4, $self['error'] . "\n"));
385  }
386  // Load the associated host record
387  list($status, $rows, $host) = ona_get_host_record(array('id' => $interface['host_id']));
388  }
389 
390  // If we didn't get a record then exit
391  if (!$host['id']) {
392  printmsg("DEBUG => Host not found ({$options['host']})!",3);
393  $self['error'] = "ERROR => Host not found ({$options['host']})!";
394  return(array(4, $self['error'] . "\n"));
395  }
396 
397  // Get related Device record info
398  list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id']));
399 
400 
401  //
402  // Define the records we're updating
403  //
404 
405  // This variable will contain the updated info we'll insert into the DB
406  $SET = array();
407 
408  // Set options['set_type']?
409  if ($options['set_type']) {
410  // Find the Device Type ID (i.e. Type) to use
411  list($status, $rows, $device_type) = ona_find_device_type($options['set_type']);
412  if ($status or $rows != 1 or !$device_type['id']) {
413  printmsg("DEBUG => The device type specified, {$options['set_type']}, does not exist!",3);
414  $self['error'] = "ERROR => The device type specified, {$options['set_type']}, does not exist!";
415  return(array(6, $self['error'] . "\n"));
416  }
417  printmsg("DEBUG => Device type ID: {$device_type['id']}", 3);
418 
419  // Everything looks ok, add it to $SET if it changed...
420  if ($device['device_type_id'] != $device_type['id'])
421  $SET_DEV['device_type_id'] = $device_type['id'];
422  }
423 
424  // Set options['set_notes'] (it can be a null string!)
425  if (array_key_exists('set_notes', $options)) {
426  // There is an issue with escaping '=' and '&'. We need to avoid adding escape characters
427  $options['set_notes'] = str_replace('\\=','=',$options['set_notes']);
428  $options['set_notes'] = str_replace('\\&','&',$options['set_notes']);
429  // If it changed...
430  if ($host['notes'] != $options['set_notes'])
431  $SET['notes'] = $options['set_notes'];
432  }
433 
434  if (array_key_exists('set_device', $options)) {
435  list($status, $rows, $devid) = ona_find_device($options['set_device']);
436  if (!$rows) {
437  printmsg("DEBUG => The device specified, {$options['set_device']}, does not exist!",3);
438  $self['error'] = "ERROR => The device specified, {$options['set_device']}, does not exist!";
439  return(array(7, $self['error'] . "\n"));
440  }
441 
442  // set the device id
443  if ($host['device_id'] != $devid['id']) $SET['device_id'] = $devid['id'];
444  }
445 
446 
447  if (array_key_exists('set_location', $options)) {
448  if (!$options['set_location'])
449  unset($SET_DEV['location_id']);
450  else {
451  list($status, $rows, $loc) = ona_find_location($options['set_location']);
452  if (!$rows) {
453  printmsg("DEBUG => The location specified, {$options['set_location']}, does not exist!",3);
454  $self['error'] = "ERROR => The location specified, {$options['set_location']}, does not exist!";
455  return(array(7, $self['error'] . "\n"));
456  }
457  // If location is changing, then set the variable
458  if ($device['location_id'] != $loc['id']) $SET_DEV['location_id'] = $loc['id'];
459  }
460  }
461 
462  // Check permissions
463  if (!auth('host_modify')) {
464  $self['error'] = "Permission denied!";
465  printmsg($self['error'], 0);
466  return(array(10, $self['error'] . "\n"));
467  }
468 
469  // Get the host record before updating (logging)
470  $original_host = $host;
471 
472  // Update the host record if necessary
473  if(count($SET) > 0) {
474  list($status, $rows) = db_update_record($onadb, 'hosts', array('id' => $host['id']), $SET);
475  if ($status or !$rows) {
476  $self['error'] = "ERROR => host_modify() SQL Query failed for host: " . $self['error'];
477  printmsg($self['error'], 0);
478  return(array(8, $self['error'] . "\n"));
479  }
480  }
481 
482  // Update device table if necessary
483  if(count($SET_DEV) > 0) {
484  list($status, $rows) = db_update_record($onadb, 'devices', array('id' => $host['device_id']), $SET_DEV);
485  if ($status or !$rows) {
486  $self['error'] = "ERROR => host_modify() SQL Query failed for device type: " . $self['error'];
487  printmsg($self['error'], 0);
488  return(array(9, $self['error'] . "\n"));
489  }
490  }
491 
492  // Get the host record after updating (logging)
493  list($status, $rows, $new_host) = ona_get_host_record(array('id' => $host['id']));
494 
495  // Return the success notice
496  $self['error'] = "INFO => Host UPDATED:{$host['id']}: {$new_host['fqdn']}";
497 
498  $log_msg = "INFO => Host UPDATED:{$host['id']}: ";
499  $more="";
500  foreach(array_keys($host) as $key) {
501  if($host[$key] != $new_host[$key]) {
502  $log_msg .= "{$more}{$key}: {$host[$key]} => {$new_host[$key]}";
503  $more= "; ";
504  }
505  }
506 
507  // only print to logfile if a change has been made to the record
508  if($more != '') {
509  printmsg($self['error'], 0);
510  printmsg($log_msg, 0);
511  }
512 
513  return(array(0, $self['error'] . "\n"));
514 }
515 
516 
517 
518 
519 
520 
521 
522 
523 
524 
526 // Function: host_del (string $options='')
527 //
528 // Input Options:
529 // $options = key=value pairs of options for this function.
530 // multiple sets of key=value pairs should be separated
531 // by an "&" symbol.
532 //
533 // Output:
534 // Returns a two part list:
535 // 1. The exit status of the function. 0 on success, non-zero on
536 // error. All errors messages are stored in $self['error'].
537 // 2. A textual message for display on the console or web interface.
538 //
539 // Example: list($status, $result) = host_del('host=test');
541 function host_del($options="") {
542  global $conf, $self, $onadb;
543  printmsg("DEBUG => host_del({$options}) called", 3);
544 
545  // Version - UPDATE on every edit!
546  $version = '1.19';
547 
548  // Parse incoming options string to an array
549  $options = parse_options($options);
550 
551  // Sanitize options[commit] (default is no)
552  $options['commit'] = sanitize_YN($options['commit'], 'N');
553 
554  // Return the usage summary if we need to
555  if ($options['help'] or !$options['host']) {
556  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
557  $self['error'] = 'ERROR => Insufficient parameters';
558  return(array(1,
559 <<<EOM
560 
562 Deletes a host, and all related records from the database
563 
564  Synopsis: host_del [KEY=VALUE] ...
565 
566  Required:
567  host=NAME[.DOMAIN] or ID Hostname or ID of the host to delete
568 
569  Optional:
570  commit=[yes|no] Commit db transaction (no)
571 
572  Notes:
573  * A host won't be deleted if it has config text records
574  * A host won't be deleted if it's configured as a dns or dhcp server
575 
576 
577 EOM
578  ));
579  }
580 
581 
582  // Find the host (and domain) record from $options['host']
583  list($status, $rows, $host) = ona_find_host($options['host']);
584  printmsg("DEBUG => host_del() Host: {$host['fqdn']} ({$host['id']})", 3);
585  if (!$host['id']) {
586  printmsg("DEBUG => Unknown host: {$host['fqdn']}",3);
587  $self['error'] = "ERROR => Unknown host: {$host['fqdn']}";
588  return(array(2, $self['error'] . "\n"));
589  }
590 
591 
592  // Check permissions
593  if (!auth('host_del') or !authlvl($host['LVL'])) {
594  $self['error'] = "Permission denied!";
595  printmsg($self['error'], 0);
596  return(array(10, $self['error'] . "\n"));
597  }
598 
599  // If "commit" is yes, delete the host
600  if ($options['commit'] == 'Y') {
601  $text = "";
602  $add_to_error = "";
603  $add_to_status = 0;
604 
605  // SUMMARY:
606  // Don't allow a delete if it is performing server duties
607  // Don't allow a delete if config text entries exist
608  // Delete Interfaces
609  // Delete interface cluster entries
610  // Delete dns records
611  // Delete custom attributes
612  // Delete DHCP entries
613  // Delete device record if it is the last host associated with it.
614  //
615  // IDEA: If it's the last host in a domain (maybe do the same for or a networks & vlans in the interface delete)
616  // It could just print a notice or something.
617 
618  // Check that it is the host is not performing server duties
619  // FIXME: MP mostly fixed..needs testing
620  $serverrow = 0;
621  // check ALL the places server_id is used and remove the entry from server_b if it is not used
622  list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_server_subnets', array('host_id' => $host['id']));
623  if ($rows) $serverrow++;
624  list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('primary_server_id' => $host['id']));
625  if ($rows) $serverrow++;
626  list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('secondary_server_id' => $host['id']));
627  if ($rows) $serverrow++;
628  if ($serverrow > 0) {
629  printmsg("DEBUG => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DHCP server!",3);
630  $self['error'] = "ERROR => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DHCP server!";
631  return(array(5, $self['error'] . "\n"));
632  }
633 
634 
635  // Check if host is a dns server
636  $serverrow = 0;
637  list($status, $rows, $srecord) = db_get_record($onadb, 'dns_server_domains', array('host_id' => $host['id']));
638  if ($rows) $serverrow++;
639 
640  if ($serverrow > 0) {
641  printmsg("DEBUG => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DNS server!",3);
642  $self['error'] = "ERROR => Host ({$host['fqdn']}) cannot be deleted, it is performing duties as a DNS server!";
643  return(array(5, $self['error'] . "\n"));
644  }
645 
646  // Display an error if it has any entries in configurations
647  list($status, $rows, $server) = db_get_record($onadb, 'configurations', array('host_id' => $host['id']));
648  if ($rows) {
649  printmsg("DEBUG => Host ({$host['fqdn']}) cannot be deleted, it has config archives!",3);
650  $self['error'] = "ERROR => Host ({$host['fqdn']}) cannot be deleted, it has config archives!";
651  return(array(5, $self['error'] . "\n"));
652  }
653 
654 
655  // Delete interface(s)
656  // get list for logging
657  $clustcount = 0;
658  $dnscount = 0;
659  list($status, $rows, $interfaces) = db_get_records($onadb, 'interfaces', array('host_id' => $host['id']));
660 
661 
662  // Cant delete if one of the interfaces is primary for a cluster
663  foreach ($interfaces as $int) {
664  list($status, $rows, $records) = db_get_records($onadb, 'interface_clusters', array('interface_id' => $int['id']));
665  $clustcount = $clustcount + $rows;
666  }
667 
668  if ($clustcount) {
669  $self['error'] = "ERROR => host_del() An interface on this host is primary for some interface shares, delete the share or move the interface first.";
670  printmsg($self['error'],0);
671  return(array(5, $self['error'] . "\n"));
672  }
673 
674  // do the interface_cluster delete. This just removes this host from the cluster, not the whole cluster itself
675  // It will error out as well if this interface is the primary in the cluster
676  list($status, $rows) = db_delete_records($onadb, 'interface_clusters', array('host_id' => $host['id']));
677  if ($status) {
678  $self['error'] = "ERROR => host_del() interface_cluster delete SQL Query failed: {$self['error']}";
679  printmsg($self['error'],0);
680  return(array(5, $self['error'] . "\n"));
681  }
682  // log deletions
683  printmsg("INFO => {$rows} Shared interface(s) DELETED from {$host['fqdn']}",0);
684  $add_to_error .= "INFO => {$rows} Shared interface(s) DELETED from {$host['fqdn']}\n";
685 
686 
687  // Delete each DNS record associated with this hosts interfaces.
688 // foreach ($interfaces as $int) {
689 // // Loop through each dns record associated with this interface.
690 // list($status, $rows, $records) = db_get_records($onadb, 'dns', array('interface_id' => $int['id']));
691 // if ($rows) {
692 // foreach($records as $record) {
693 // // Run the module
694 // list($status, $output) = run_module('dns_record_del', array('name' => $record['id'], 'type' => $record['type'], 'commit' => 'Y', 'delete_by_module' => 'Y'));
695 // $add_to_error .= $output;
696 // $add_to_status = $add_to_status + $status;
697 // }
698 // }
699 // }
700 
701  // Delete messages
702  // get list for logging
703  list($status, $rows, $records) = db_get_records($onadb, 'messages', array('table_name_ref' => 'hosts','table_id_ref' => $host['id']));
704  // do the delete
705  list($status, $rows) = db_delete_records($onadb, 'messages', array('table_name_ref' => 'hosts','table_id_ref' => $host['id']));
706  if ($status) {
707  $self['error'] = "ERROR => host_del() message delete SQL Query failed: {$self['error']}";
708  printmsg($self['error'],0);
709  return(array(5, $self['error'] . "\n"));
710  }
711  // log deletions
712  printmsg("INFO => {$rows} Message(s) DELETED from {$host['fqdn']}",0);
713  $add_to_error .= "INFO => {$rows} Message(s) DELETED from {$host['fqdn']}\n";
714 
715 
716  // Delete the interfaces.. this should delete dns names and other things associated with interfaces..
717  foreach ($interfaces as $record) {
718  // Run the module
719  list($status, $output) = run_module('interface_del', array('interface' => $record['id'], 'commit' => 'on', 'delete_by_module' => 'Y'));
720  $add_to_error .= $output;
721  $add_to_status = $add_to_status + $status;
722  }
723 
724 
725 
726 
727  // Delete device record
728  // Count how many hosts use this same device
729  list($status, $rows, $records) = db_get_records($onadb, 'hosts', array('device_id' => $host['device_id']));
730  // if device count is just 1 do the delete
731  if ($rows == 1) {
732  list($status, $rows) = db_delete_records($onadb, 'devices', array('id' => $host['device_id']));
733  if ($status) {
734  $self['error'] = "ERROR => host_del() device delete SQL Query failed: {$self['error']}";
735  printmsg($self['error'],0);
736  return(array(5, $add_to_error . $self['error'] . "\n"));
737  }
738  // log deletions
739  printmsg("INFO => Device record DELETED: [{$record['id']}] no remaining hosts using this device",0);
740  } else {
741  printmsg("INFO => Device record NOT DELETED: [{$record['id']}] there are other hosts using this device.",1);
742  }
743 
744 
745  // Delete tag entries
746  list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
747  $log=array(); $i=0;
748  foreach ($records as $record) {
749  $log[$i]= "INFO => Tag DELETED: {$record['name']} from {$host['fqdn']}";
750  $i++;
751  }
752  //do the delete
753  list($status, $rows) = db_delete_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
754  if ($status) {
755  $self['error'] = "ERROR => host_del() Tag delete SQL Query failed: {$self['error']}";
756  printmsg($self['error'],0);
757  return(array(5, $add_to_error . $self['error'] . "\n"));
758  }
759  //log deletions
760  foreach($log as $log_msg) {
761  printmsg($log_msg,0);
762  $add_to_error .= $log_msg . "\n";
763  }
764 
765  // Delete custom attribute entries
766  // get list for logging
767  list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
768  $log=array(); $i=0;
769  foreach ($records as $record) {
770  list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id']));
771  $log[$i]= "INFO => Custom Attribute DELETED: {$ca['name']} ({$ca['value']}) from {$host['fqdn']}";
772  $i++;
773  }
774 
775  //do the delete
776  list($status, $rows) = db_delete_records($onadb, 'custom_attributes', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
777  if ($status) {
778  $self['error'] = "ERROR => host_del() Custom attribute delete SQL Query failed: {$self['error']}";
779  printmsg($self['error'],0);
780  return(array(5, $add_to_error . $self['error'] . "\n"));
781  }
782 
783  //log deletions
784  foreach($log as $log_msg) {
785  printmsg($log_msg,0);
786  $add_to_error .= $log_msg . "\n";
787  }
788 
789 
790 
791 
792  // Delete DHCP options
793  // get list for logging
794  list($status, $rows, $records) = db_get_records($onadb, 'dhcp_option_entries', array('host_id' => $host['id']));
795  $log=array(); $i=0;
796  foreach ($records as $record) {
797  list($status, $rows, $dhcp) = ona_get_dhcp_option_entry_record(array('id' => $record['id']));
798  $log[$i]= "INFO => DHCP entry DELETED: {$dhcp['display_name']}={$dhcp['value']} from {$host['fqdn']}";
799  $i++;
800  }
801  // do the delete
802  list($status, $rows) = db_delete_records($onadb, 'dhcp_option_entries', array('host_id' => $host['id']));
803  if ($status) {
804  $self['error'] = "ERROR => host_del() DHCP option entry delete SQL Query failed: {$self['error']}";
805  printmsg($self['error'],0);
806  return(array(5, $add_to_error . $self['error'] . "\n"));
807  }
808  // log deletions
809  foreach($log as $log_msg) {
810  printmsg($log_msg,0);
811  $add_to_error .= $log_msg . "\n";
812  }
813 
814  // Delete the host
815  list($status, $rows) = db_delete_records($onadb, 'hosts', array('id' => $host['id']));
816  if ($status) {
817  $self['error'] = "ERROR => host_del() host delete SQL Query failed: {$self['error']}";
818  printmsg($self['error'],0);
819  return(array(5, $add_to_error . $self['error'] . "\n"));
820  }
821 
822  // Return the success notice
823  if ($add_to_status == 0) $self['error'] = "INFO => Host DELETED: {$host['fqdn']}";
824  printmsg($self['error'], 0);
825  return(array($add_to_status, $add_to_error . $self['error'] . "\n"));
826  }
827 
828 
829  //
830  // We are just displaying records that would have been deleted
831  //
832 
833  // SUMMARY:
834  // Display a warning if it is a server
835  // Display a warning if it has config text entries
836  // Display Interfaces
837  // Display dns records
838  // Display custom attributes
839  // Display DHCP entries
840 
841  // Otherwise just display the host record for the host we would have deleted
842  $text = "Record(s) NOT DELETED (see \"commit\" option)\n" .
843  "Displaying record(s) that would have been deleted:\n";
844 
845  // Display a warning if host is performing server duties
846  list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_server_subnets', array('host_id' => $host['id']));
847  if ($rows) {
848  $text .= "\nWARNING! This host is a DHCP server for {$rows} subnet(s)\n";
849  }
850  list($status, $rows, $srecord) = db_get_record($onadb, 'dns_server_domains', array('host_id' => $host['id']));
851  if ($rows) {
852  $text .= "\nWARNING! This host is a DNS server for one or more domains!\n";
853  }
854  list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('primary_server_id' => $host['id']));
855  if ($rows) {
856  $text .= "\nWARNING! This host is a server that is primary in a DHCP failover group\n";
857  }
858  list($status, $rows, $srecord) = db_get_record($onadb, 'dhcp_failover_groups', array('secondary_server_id' => $host['id']));
859  if ($rows) {
860  $text .= "\nWARNING! This host is a server that is secondary in a DHCP failover group\n";
861  }
862 
863  // Display a warning if it has any configurations
864  list($status, $rows, $server) = db_get_record($onadb, 'configurations', array('host_id' => $host['id']));
865  if ($rows)
866  $text .= "\nWARNING! Host can not be deleted, it has config archives!\n";
867 
868  if ($rows)
869  $text .= "\nWARNING! Host will NOT be deleted, due to previous warnings!\n";
870 
871  // Display the Host's complete record
872  list($status, $tmp) = host_display("host={$host['id']}&verbose=N");
873  $text .= "\n" . $tmp;
874 
875  // Display count of messages
876  list($status, $rows, $records) = db_get_records($onadb, 'messages', array('table_name_ref' => 'hosts','table_id_ref' => $host['id']));
877  if ($rows) $text .= "\nASSOCIATED MESSAGE RECORDS ({$rows}):\n";
878 
879  // Display associated interface(s)
880  list($status, $int_rows, $interfaces) = db_get_records($onadb, 'interfaces', array('host_id' => $host['id']));
881  // show the dns records associated
882  foreach ($interfaces as $record) {
883  list($status, $rows, $dnsrec) = db_get_records($onadb, 'dns', array('interface_id' => $record['id']));
884  if ($rows) {
885  $text .= "\nASSOCIATED DNS RECORDS ({$rows}) ON INTERFACE (" . ip_mangle($record['ip_addr'], 'dotted') . "):\n";
886  foreach ($dnsrec as $rec) {
887  // show AAAA or A type as needed
888  if ($record['ip_addr'] > 4294967295 and $rec['type'] == 'A') $rec['type'] = 'AAAA';
889  $text .= " TYPE: [ID:{$rec['id']}] {$rec['type']}, {$rec['name']} -> " . ip_mangle($record['ip_addr'], 'dotted') . "\n";
890  }
891  }
892  }
893 
894  if ($int_rows) $text .= "\nASSOCIATED INTERFACE RECORDS ({$int_rows}):\n";
895  foreach ($interfaces as $record) {
896  $text .= " [ID:{$record['id']}] " . ip_mangle($record['ip_addr'], 'dotted') . "\n";
897  }
898 
899  // Display associated interface_clusters(s)
900  list($status, $clust_rows, $interfaceclusters) = db_get_records($onadb, 'interface_clusters', array('host_id' => $host['id']));
901 
902  if ($clust_rows) $text .= "\nASSOCIATED SHARED INTERFACE RECORDS ({$clust_rows}):\n";
903  foreach ($interfaceclusters as $record) {
904  list($status, $rows, $int) = ona_get_interface_record(array('id' => $record['interface_id']));
905  $text .= " [ID:{$int['id']}] {$int['ip_addr_text']}\n";
906  }
907 
908  // Display associated tags
909  list($status, $rows, $records) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
910  if ($rows) $text .= "\nASSOCIATED TAG RECORDS ({$rows}):\n";
911  foreach ($records as $record) {
912  $text .= " {$record['name']}\n";
913  }
914 
915  // Display associated custom attributes
916  list($status, $rows, $records) = db_get_records($onadb, 'custom_attributes', array('table_name_ref' => 'hosts', 'table_id_ref' => $host['id']));
917  if ($rows) $text .= "\nASSOCIATED CUSTOM ATTRIBUTE RECORDS ({$rows}):\n";
918  foreach ($records as $record) {
919  list($status, $rows, $ca) = ona_get_custom_attribute_record(array('id' => $record['id']));
920  $text .= " {$ca['name']} => {$ca['value']}\n";
921  }
922 
923  // Display associated DHCP entries
924  list($status, $rows, $records) = db_get_records($onadb, 'dhcp_option_entries', array('host_id' => $host['id']));
925  if ($rows) $text .= "\nASSOCIATED DHCP OPTION RECORDS ({$rows}):\n";
926  foreach ($records as $record) {
927  list($status, $rows, $dhcp) = ona_get_dhcp_option_entry_record(array('id' => $record['id']));
928  $text .= " {$dhcp['display_name']} => {$dhcp['value']}\n";
929  }
930 
931 
932 
933  return(array(7, $text));
934 }
935 
936 
937 
938 
939 
940 
941 
942 
943 
944 
945 
947 // Function: host_display (string $options='')
948 //
949 // $options = key=value pairs of options for this function.
950 // multiple sets of key=value pairs should be separated
951 // by an "&" symbol.
952 //
953 // Input Options:
954 // host=HOSTNAME[.DOMAIN] or ID
955 //
956 // Output:
957 // Returns a two part list:
958 // 1. The exit status of the function. 0 on success, non-zero on
959 // error. All errors messages are stored in $self['error'].
960 // 2. A textual message for display on the console or web interface.
961 //
962 // Example: list($status, $result) = host_display('host=test');
964 function host_display($options="") {
965  global $conf, $self, $onadb;
966 
967  $text_array = array();
968 
969  // Version - UPDATE on every edit!
970  $version = '1.04';
971 
972  printmsg("DEBUG => host_display({$options}) called", 3);
973 
974  // Parse incoming options string to an array
975  $options = parse_options($options);
976 
977  // Sanitize options[verbose] (default is yes)
978  $options['verbose'] = sanitize_YN($options['verbose'], 'Y');
979 
980  // Return the usage summary if we need to
981  if ($options['help'] or !$options['host'] ) {
982  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
983  $self['error'] = 'ERROR => Insufficient parameters';
984  return(array(1,
985 <<<EOM
986 
988 Displays a host record from the database
989 
990  Synopsis: host_display [KEY=VALUE] ...
991 
992  Required:
993  host=NAME[.DOMAIN] or ID hostname or ID of the host display
994 
995  Optional:
996  verbose=[yes|no] display additional info (yes)
997 
998 \n
999 EOM
1000 
1001  ));
1002  }
1003 
1004 
1005  // Find the host (and domain) record from $options['host']
1006  list($status, $rows, $host) = ona_find_host($options['host']);
1007  printmsg("DEBUG => Host: {$host['fqdn']}", 3);
1008  if (!$host['id']) {
1009  printmsg("DEBUG => Unknown host: {$options['host']}",3);
1010  $self['error'] = "ERROR => Unknown host: {$options['host']}";
1011  return(array(2, $self['error'] . "\n"));
1012  }
1013 
1014  $text_array = $host;
1015 
1016  // Build text to return
1017  $text = "HOST RECORD ({$host['fqdn']})\n";
1018  $text .= format_array($host);
1019 
1020  // If 'verbose' is enabled, grab some additional info to display
1021  if ($options['verbose'] == 'Y') {
1022 
1023 // TODO: if it is a nat interface, maybe process that IP and make it visible?
1024 
1025  // Interface record(s)
1026  $i = 0;
1027  do {
1028  list($status, $rows, $interface) = ona_get_interface_record(array('host_id' => $host['id']));
1029  if ($rows == 0) { break; }
1030  $i++;
1031  $text .= "\nASSOCIATED INTERFACE RECORD ({$i} of {$rows})\n";
1032  $text .= format_array($interface);
1033  $text_array['interfaces'][$i] = $interface;
1034  unset($text_array['interfaces'][$i]['host_id']);
1035  } while ($i < $rows);
1036 
1037  $text_array['interface_count'] = $rows;
1038 
1039  // Device record
1040  list($status, $rows, $device) = ona_get_device_record(array('id' => $host['device_id']));
1041  if ($rows >= 1) {
1042  // Fill out some other device info
1043  list($status, $rows, $device_type) = ona_get_device_type_record(array('id' => $device['device_type_id']));
1044  list($status, $rows, $role) = ona_get_role_record(array('id' => $device_type['role_id']));
1045  list($status, $rows, $model) = ona_get_model_record(array('id' => $device_type['model_id']));
1046  list($status, $rows, $manufacturer) = ona_get_manufacturer_record(array('id' => $model['manufacturer_id']));
1047  $device['device_type'] = "{$manufacturer['name']}, {$model['name']} ({$role['name']})";
1048 
1049  list($status, $rows, $location) = ona_get_location_record(array('id' => $device['location_id']));
1050 
1051  $text_array['location'] = $location;
1052  $text_array['device'] = $device;
1053  $text .= "\nASSOCIATED DEVICE RECORD\n";
1054  $text .= format_array($device);
1055  }
1056 
1057  // Tag records
1058  list($status, $rows, $tags) = db_get_records($onadb, 'tags', array('type' => 'host', 'reference' => $host['id']));
1059  if ($rows) {
1060  $text .= "\nASSOCIATED TAG RECORDS\n";
1061  foreach ($tags as $tag) {
1062  $text_array['tags'][] = $tag['name'];
1063  $text .= " {$tag['name']}\n";
1064  }
1065  }
1066 
1067  }
1068 
1069  // Cleanup unused info
1070  unset($text_array['device_id']);
1071  unset($text_array['device']['asset_tag']);
1072  unset($text_array['device']['location_id']);
1073  unset($text_array['device']['serial_number']);
1074 
1075  // change the output format if other than default
1076  if ($options['format'] == 'json') {
1077  $text = $text_array;
1078  }
1079  if ($options['format'] == 'yaml') {
1080  $text = $text_array;
1081  }
1082 
1083  // Return the success notice
1084  return(array(0, $text));
1085 
1086 }
1087 
1088 
1089 
1090 
1091 
1092 
1093 
1094 
1095 // DON'T put whitespace at the beginning or end of this file!!!
1096 ?>
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
ona_get_host_record
ona_get_host_record($array='', $order='')
Definition: functions_db.inc.php:1106
ona_get_manufacturer_record
ona_get_manufacturer_record($array)
Definition: functions_db.inc.php:1210
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
ona_get_location_record
ona_get_location_record($array='', $order='')
Definition: functions_db.inc.php:1122
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
host_del
host_del($options="")
Definition: host.inc.php:541
ona_find_domain
ona_find_domain($fqdn="", $returndefault=0)
Definition: functions_db.inc.php:1609
ona_get_device_record
ona_get_device_record($array)
Definition: functions_db.inc.php:1218
Name
Full Name
Definition: app_user_info.inc.php:101
ip_mangle
ip_mangle($ip="", $format="default")
Definition: functions_general.inc.php:308
ona_get_interface_record
ona_get_interface_record($array='', $order='')
Definition: functions_db.inc.php:1126
ona_find_host
ona_find_host($search="")
Definition: functions_db.inc.php:1490
host_display
host_display($options="")
Definition: host.inc.php:964
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
db_get_records
db_get_records($dbh=0, $table="", $where="", $order="", $rows=-1, $offset=-1)
Definition: functions_db.inc.php:891
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
ona_find_device_type
ona_find_device_type($search="")
Definition: functions_db.inc.php:2184
ona_get_role_record
ona_get_role_record($array)
Definition: functions_db.inc.php:1222
ona_get_device_type_record
ona_get_device_type_record($array)
Definition: functions_db.inc.php:1214
db_delete_records
db_delete_records($dbh=0, $table="", $where="")
Definition: functions_db.inc.php:582
ona_get_model_record
ona_get_model_record($array)
Definition: functions_db.inc.php:1197
printmsg
if(6<=$conf['debug']) printmsg($msg="", $debugLevel=0)
Definition: functions_general.inc.php:48
format_array
format_array($array=array())
Definition: functions_general.inc.php:1745
ona_find_interface
ona_find_interface($search="")
Definition: functions_db.inc.php:1910
ona_find_location
ona_find_location($search="")
Definition: functions_db.inc.php:1810
ona_get_custom_attribute_record
ona_get_custom_attribute_record($array)
Definition: functions_db.inc.php:1183
$host
$host
Definition: collate-convert.php:19
if
if(!is_dir( $include))
Definition: 2-to-3.php:11
db_update_record
db_update_record($dbh=0, $table="", $where="", $insert="")
Definition: functions_db.inc.php:474
run_module
run_module($module='', $options='', $transaction=1)
Definition: functions_general.inc.php:1468
ona_find_device
ona_find_device($search="")
Definition: functions_db.inc.php:2105
sanitize_hostname
sanitize_hostname($string="")
Definition: functions_general.inc.php:900
$conf
global $conf
Definition: 2-to-3.php:15
$output
$output
Definition: dcm.php:16
$text
$text
Definition: install.php:11
$self
global $self
Definition: 2-to-3.php:15
host_modify
host_modify($options="")
Definition: host.inc.php:321
db_get_record
db_get_record($dbh=0, $table="", $where="", $order="")
Definition: functions_db.inc.php:708
auth
auth($resource, $msg_level=1)
Definition: functions_general.inc.php:1330
ona_get_dhcp_option_entry_record
ona_get_dhcp_option_entry_record($array)
Definition: functions_db.inc.php:1262
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22
host_add
host_add($options="")
Definition: host.inc.php:24
sanitize_security_level
sanitize_security_level($string="", $default=-1)
Definition: functions_general.inc.php:860
ona_get_dns_record
ona_get_dns_record($array='', $order='')
Definition: functions_db.inc.php:1142