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)  

configuration.inc.php
Go to the documentation of this file.
1 <?php
2 
3 // Make sure we have necessary functions & DB connectivity
4 require_once($conf['inc_functions_db']);
5 
6 
7 
8 
10 // Function: find_config (array $search)
11 //
12 // Input Options:
13 // $search = See functions below for how this function is used.
14 // It's an internal function.
15 //
16 // Output:
17 // Returns the data returned from get_config_record()
18 // Error messages are stored in global $self['error']
19 //
21 function find_config($options=array()) {
22 
23  global $self;
24 
25  $status = 1;
26  $rows = 0;
27  $config = array();
28 
29  // If the user specified a config text ID
30  if ($options['config']) {
31  if (!preg_match('/^\d+$/', $options['config'])) {
32  $self['error'] = "ERROR => A non-digit config ID was specified!";
33  return(array(2, 0, array()));
34  }
35 
36  list($status, $rows, $config) = ona_get_config_record(array('id' => $options['config']));
37  }
38 
39  // Otherwise we're selecting a config by hostname and type
40  else if ($options['host'] and $options['type']) {
41  // Search for the host first
42  list($status, $rows, $host) = ona_find_host($options['host']);
43 
44  // Error if the host doesn't exist
45  if (!$host['id']) {
46  $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
47  return(array(3, 0, array()));
48  }
49 
50  // Now find the ID of the config type they entered
51  list($status, $rows, $config_type) = ona_get_config_type_record(array('name' => $options['type']));
52  if (!$config_type['id']) {
53  $self['error'] = "ERROR => The config type specified, {$options['type']}, is invalid!";
54  return(array(4, 0, array()));
55  }
56 
57  // Select the first config record of the specified type and host
58  list($status, $rows, $config) = ona_get_config_record(array('host_id' => $host['id'],
59  'configuration_type_id' => $config_type['id']));
60  }
61 
62  // Return the config record we got
63  return(array($status, $rows, $config));
64 
65 }
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
77 // Function: config_display (string $options='')
78 //
79 // Input Options:
80 // $options = key=value pairs of options for this function.
81 // multiple sets of key=value pairs should be separated
82 // by an "&" symbol.
83 //
84 // Output:
85 // Returns a two part list:
86 // 1. The exit status of the function (0 on success, non-zero on error)
87 // 2. A textual message displaying information on the selected unit
88 // record from the database.
89 //
90 // Example: list($status, $text) = config_display('config=12345');
91 //
92 // Exit codes:
93 // 0 :: No error
94 // 1 :: Help text printed - Insufficient or invalid input received
95 // 2 :: No config text entries found!
97 function config_display($options="") {
98 
99  // The important globals
100  global $conf;
101  global $self;
102  global $onadb;
103 
104  // Version - UPDATE on every edit!
105  $version = '1.02';
106 
107  printmsg('DEBUG => config_display('.$options.') called', 3);
108 
109  // Parse incoming options string to an array
110  $options = parse_options($options);
111 
112  // Set "options[verbose] to yes if it's not set
113  if (!array_key_exists('verbose', $options)) {
114  $options['verbose'] = 'Y';
115  }
116  // Otherwise sanitize it's value
117  else {
118  $options['verbose'] = sanitize_YN($options['verbose']);
119  }
120 
121  // Return the usage summary if we need to
122  if ($options['help'] or ( (!$options['config']) and (!$options['host'] or !$options['type']) ) ) {
123  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
124  return(array(1,
125 <<<EOM
126 
128 Displays a config text record from the database
129 
130  Synopsis: config_display [KEY=VALUE] ...
131 
132  Required:
133  config=ID display config by record ID
134  - or -
135  host=ID or NAME[.DOMAIN] display most recent config for specified host
136  type=TYPE type of config to display -
137  usually "IOS_VERSION" or "IOS_CONFIG"
138  Optional:
139  verbose=[yes|no] display entire record (yes)
140  "no" displays only the actual config text
141 \n
142 EOM
143 
144  ));
145  }
146 
147 
148  // Get a config record if there is one
149  $self['error'] = "";
150  list($status, $rows, $config) = ona_find_config($options);
151 
152  // Error if an error was returned
153  if ($status or !$config['id']) {
154  $text = "";
155  if ($self['error']) { $text = $self['error'] . "\n"; }
156  $text .= "ERROR => No config text entries found!\n";
157  return(array(2, $text));
158  }
159 
160  // If 'verbose' is enabled, we display the entire record
161  if ($options['verbose'] == 'Y') {
162  // Build text to return
163  $text = "CONFIG TEXT RECORD (1 of {$rows})\n";
164  $text .= format_array($config);
165  }
166 
167  // Otherwise we return only the actual config text
168  else {
169  $text = $config['config_body'];
170  }
171 
172  // Return the success notice
173  return(array(0, $text));
174 
175 }
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
187 // Function: config_chksum (string $options='')
188 //
189 // Input Options:
190 // $options = key=value pairs of options for this function.
191 // multiple sets of key=value pairs should be separated
192 // by an "&" symbol.
193 //
194 // Output:
195 // Returns a two part list:
196 // 1. The exit status of the function (0 on success, non-zero on error)
197 // 2. A textual message displaying information on the selected unit
198 // record from the database.
199 //
200 // Example: list($status, $result) = config_chksum('config=12345');
201 //
202 // Exit codes:
203 // 0 :: No error
204 // 1 :: Help text printed - Insufficient or invalid input received
205 // 2 :: No config text entries found!
207 function config_chksum($options="") {
208 
209  // The important globals
210  global $conf;
211  global $self;
212  global $onadb;
213 
214  // Version - UPDATE on every edit!
215  $version = '1.00';
216 
217  printmsg('DEBUG => config_chksum('.$options.') called', 3);
218 
219  // Parse incoming options string to an array
220  $options = parse_options($options);
221 
222  // Return the usage summary if we need to
223  if ($options['help'] or ( (!$options['config']) and (!$options['host'] or !$options['type']) ) ) {
224  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
225  return(array(1,
226 <<<EOM
227 
229 Displays the chksum of a config text record from the database
230 
231  Synopsis: config_chksum [KEY=VALUE] ...
232 
233  Required:
234  config=ID display config by record ID
235  - or -
236  host=ID or NAME[.DOMAIN] display most recent config for specified host
237  type=TYPE type of config to display -
238  usually "IOS_VERSION" or "IOS_CONFIG"
239 \n
240 EOM
241 
242  ));
243  }
244 
245  // Get a config record if there is one
246  $self['error'] = "";
247  list($status, $rows, $config) = ona_find_config($options);
248 
249  // Error if an error was returned
250  if ($status or !$config['id']) {
251  $text = "";
252  if ($self['error']) { $text = $self['error'] . "\n"; }
253  $text .= "ERROR => No config text entries found!\n";
254  return(array(2, $text));
255  }
256 
257  // Build text to return
258  $text = $config['md5_checksum'] . "\n";
259 
260  // Return the success notice
261  return(array(0, $text));
262 
263 }
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
277 // Function: config_add (string $options='')
278 //
279 // Input Options:
280 // $options = key=value pairs of options for this function.
281 // multiple sets of key=value pairs should be separated
282 // by an "&" symbol.
283 //
284 // Output:
285 // Returns a two part list:
286 // 1. The exit status of the function (0 on success, non-zero on error)
287 // 2. A textual message displaying information on the selected unit
288 // record from the database.
289 //
290 // Example: list($status, $result) = config_chksum('config=12345');
291 //
292 // Exit codes:
293 // 0 :: No error
294 // 1 :: Help text printed - Insufficient or invalid input received
295 // 2 :: Host specified doesn't exist
296 // 3 :: Invalid config type specified
297 // 4 :: The ona_get_next_id() call failed
298 // 5 :: SQL INSERT failed
300 function config_add($options="") {
301 
302  // The important globals
303  global $conf;
304  global $self;
305  global $onadb;
306 
307  // Version - UPDATE on every edit!
308  $version = '1.00';
309 
310  // This debug is set very high as it can contain large configs and sensitive data, you gotta mean it!
311  printmsg('DEBUG => config_add('.$options.') called', 7);
312 
313  // Parse incoming options string to an array
314  $options = parse_options($options);
315 
316  // Return the usage summary if we need to
317  if ($options['help'] or !($options['host'] and $options['type'] and $options['config']) ) {
318  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
319  return(array(1,
320 <<<EOM
321 
323 Adds a new config text record into the database
324 
325  Synopsis: config_add [KEY=VALUE] ...
326 
327  Required:
328  config=TEXT the actual config text or filename to insert
329  host=ID or NAME[.DOMAIN] host the config text is from
330  type=TYPE type of config text we're inserting -
331  usually "IOS_VERSION" or "IOS_CONFIG"
332 \n
333 EOM
334 
335  ));
336  }
337 
338 
339  // Search for the host first
340  list($status, $rows, $host) = ona_find_host($options['host']);
341 
342  // Error if the host doesn't exist
343  if (!$host['id']) {
344  $self['error'] = "ERROR => The host specified, {$options['host']}, does not exist!";
345  return(array(2, $self['error']));
346  }
347 
348  // Now find the ID of the config type they entered
349  list($status, $rows, $config_type) = ona_get_config_type_record(array('name' => $options['type']));
350  if (!$config_type['id']) {
351  $self['error'] = "ERROR => The config type specified, {$options['type']}, is invalid!";
352  return(array(3, $self['error']));
353  }
354 
355  $options['config'] = preg_replace('/\\\"/','"',$options['config']);
356  $options['config'] = preg_replace('/\\\=/','=',$options['config']);
357  // Get the next ID for the new config_text record
358  $id = ona_get_next_id('configurations');
359  if (!$id) {
360  return(array(4, "ERROR => The ona_get_next_id(configurations) call failed!\n"));
361  }
362  printmsg("DEBUG => ID for new config_record: $id", 3);
363 
364  // Add the config_text
365  list($status, $rows) = db_insert_record(
366  $onadb,
367  'configurations',
368  array(
369  'id' => $id,
370  'configuration_type_id' => $config_type['id'],
371  'host_id' => $host['id'],
372  'md5_checksum' => md5($options['config']),
373  'config_body' => $options['config']
374  )
375  );
376  if ($status or !$rows) {
377  $self['error'] = "ERROR => message_add() SQL Query failed: " . $self['error'];
378  printmsg($self['error'], 0);
379  return(array(6, $self['error'] . "\n"));
380  }
381 
382  list($status, $rows, $record) = ona_get_config_record(array('id' => $id));
383  if ($status or !$rows) {
384  $self['error'] = 'ERROR => SQL INSERT failed. Database error: ' . $error . "\n";
385  return(array(5, $self['error']));
386  }
387 
388  // Return the success notice
389  $text = "NOTICE => Config text record ADDED, ID: {$id}\n";
390  return(array(0, $text));
391 
392 }
393 
394 
395 
396 
397 
398 
399 
400 
401 /*
402  Function: config_diff (string $options='')
403 
404  Input Options:
405  $options = key=value pairs of options for this function.
406  multiple sets of key=value pairs should be separated
407  by an "&" symbol.
408 
409  Output:
410  Returns a two part list:
411  1. The exit status of the function (0 on success, non-zero on error)
412  2. A textual message displaying information on the selected unit
413  record from the database.
414 
415  Example: list($status, $text) = config_diff('config=12345');
416 
417  Exit codes:
418  0 :: No error
419  1 :: Help text printed - Insufficient or invalid input received
420  2 :: No config text entries found!
421 */
422 function config_diff($options="") {
423 
424  // The important globals
425  global $conf;
426  global $self;
427  global $onadb;
428 
429  // Version - UPDATE on every edit!
430  $version = '1.03';
431 
432  printmsg('DEBUG => config_diff('.$options.') called', 3);
433 
434  // Parse incoming options string to an array
435  $options = parse_options($options);
436 
437  // Return the usage summary if we need to
438  if ($options['help'] or ( !$options['host'] or !$options['type'] ) and ( !$options['ida'] or !$options['idb'] ) ) {
439  // NOTE: Help message lines should not exceed 80 characters for proper display on a console
440  return(array(1,
441 <<<EOM
442 
444 Displays the difference between selected archive entries
445 
446  Synopsis: config_diff [KEY=VALUE] ...
447 
448  Required:
449  host=ID or NAME[.DOMAIN] display most recent config for specified host
450  type=TYPE type of config to display -
451  usually "IOS_VERSION" or "IOS_CONFIG"
452  OR
453  ida=ID First config ID to compare against idb
454  idb=ID Second config ID to compare against ida
455 
456  Note:
457  If you don't pass any IDs you will get the two most recent configs
458  related to the host/type you provide.
459 
460 EOM
461  ));
462  }
463 
464  $text = "";
465 
466  // Compare arbitrary configs based on config IDs
467  // If we have ids, lets use those instead
468  if ($options['ida'] and $options['idb']) {
469  // get the two configs from the db
470  list($status, $rows, $configs) = db_get_records($onadb,'configurations',
471  "id in ({$options['ida']},{$options['idb']})",
472  'ctime DESC',
473  '2',
474  ''
475  );
476  } else {
477  // Get a config record if there is one
478  $self['error'] = "";
479  list($status, $rows, $config) = ona_find_config($options);
480  list($status, $rows, $configs) = db_get_records($onadb,'configurations',
481  array('host_id' => $config['host_id'],'configuration_type_id' => $config['configuration_type_id']),
482  'ctime DESC',
483  '2',
484  ''
485  );
486  }
487 
488  // Error if an error was returned
489  if ($status or $rows != 2) {
490  if ($self['error']) { $text = $self['error'] . "\n"; }
491  $text .= "ERROR => One or more config text entries not found!\n";
492  return(array(2, $text));
493  }
494 
495  // Get a unified text diff output
496  $text .= text_diff($configs[1]['config_body'], $configs[0]['config_body']);
497 
498  // Return the success notice
499  return(array(0, $text));
500 
501 }
502 
503 
504 ?>
config_diff
config_diff($options="")
Definition: configuration.inc.php:422
db_insert_record
db_insert_record($dbh=0, $table="", $insert="")
Definition: functions_db.inc.php:375
sanitize_YN
sanitize_YN($string="", $default="Y")
Definition: functions_general.inc.php:1637
ona_get_next_id
ona_get_next_id($tablename)
Definition: functions_db.inc.php:1369
config_chksum
config_chksum($options="")
Definition: configuration.inc.php:207
ona_find_host
ona_find_host($search="")
Definition: functions_db.inc.php:1490
$record
$record['display_name']
Definition: app_advanced_search.inc.php:12
$status
$status
Definition: install.php:12
$onadb
global $onadb
Definition: 2-to-3.php:15
config_add
config_add($options="")
Definition: configuration.inc.php:300
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_config
ona_find_config($options=array())
Definition: functions_db.inc.php:2524
$host
$host
Definition: collate-convert.php:19
config_display
config_display($options="")
Definition: configuration.inc.php:97
$conf
global $conf
Definition: 2-to-3.php:15
$text
$text
Definition: install.php:11
find_config
find_config($options=array())
Definition: configuration.inc.php:21
$self
global $self
Definition: 2-to-3.php:15
ona_get_config_type_record
ona_get_config_type_record($array='', $order='')
Definition: functions_db.inc.php:1171
ona_get_config_record
ona_get_config_record($array='', $order='ctime DESC')
Definition: functions_db.inc.php:1160
parse_options
parse_options($options="")
Definition: functions_general.inc.php:1579
$version
$version
Definition: main.inc.php:22