array( 'filter' => FILTER_VALIDATE_INT, 'pageset' => true, 'default' => '-1' ), 'paged' => array( 'filter' => FILTER_VALIDATE_INT, 'default' => '1' ), 'host_status' => array( 'filter' => FILTER_VALIDATE_INT, 'pageset' => true, 'default' => '-1' ), 'host_template_id' => array( 'filter' => FILTER_VALIDATE_INT, 'pageset' => true, 'default' => '-1' ), 'filterd' => array( 'filter' => FILTER_DEFAULT, 'pageset' => true, 'default' => '' ), 'sort_column' => array( 'filter' => FILTER_CALLBACK, 'default' => 'description', 'options' => array('options' => 'sanitize_search_string') ), 'sort_direction' => array( 'filter' => FILTER_CALLBACK, 'default' => 'ASC', 'options' => array('options' => 'sanitize_search_string') ), 'has_graphs' => array( 'filter' => FILTER_VALIDATE_REGEXP, 'options' => array('options' => array('regexp' => '(true|false)')), 'pageset' => true, 'default' => 'true' ) ); validate_store_request_vars($filters, 'sess_auto'); /* ================= input validation ================= */ if (isset_request_var('cleard')) { unset_request_var('clear'); } /* if the number of rows is -1, set it to the default */ if (get_request_var('rowsd') == -1) { $rows = read_config_option('num_rows_table'); } else { $rows = get_request_var('rowsd'); } if ((!empty($_SESSION['sess_automation_host_status'])) && (!isempty_request_var('host_status'))) { if ($_SESSION['sess_automation_host_status'] != get_request_var('host_status')) { set_request_var('paged', '1'); } } ?>
| '> | '> '> |
'; print_r($item); print ''; $field_name = ($item['field'] === AUTOMATION_TREE_ITEM_TYPE_STRING) ? $automation_tree_header_types[AUTOMATION_TREE_ITEM_TYPE_STRING] : $item['field']; form_alternate_row(); $form_data = '
'; print $sql_query; print''; cacti_log($function . ' returns: ' . $sql_query, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); return $sql_query; } function build_matching_objects_filter($rule_id, $rule_type) { $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . " called rule id: $rule_id", false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); $sql_filter = ''; /* create an SQL which queries all host related tables in a huge join * this way, we may add any where clause that might be added via * 'Matching Device' match */ $rule_items = db_fetch_assoc_prepared('SELECT * FROM automation_match_rule_items WHERE rule_id = ? AND rule_type = ? ORDER BY sequence', array($rule_id, $rule_type)); #print '
Items: $sql'; if (cacti_sizeof($rule_items)) { # $sql_order = build_sort_order($xml_array['index_order_type'], 'automation_host'); # $sql_query = build_data_query_sql($rule); $sql_filter = build_rule_item_filter($rule_items); # print 'SQL Query: ' . $sql_query . '
'; print_r($rule_items); print '
'; print_r($fields); print ''; # remove unwanted entries $fields = array_minus($fields, $excluded_fields); # now reformat entries for use with draw_edit_form if (cacti_sizeof($fields)) { foreach ($fields as $key => $value) { switch($table) { case 'graph_templates_graph': $table = 'gtg'; break; case 'host': $table = 'h'; break; case 'host_template': $table = 'ht'; break; case 'graph_templates': $table = 'gt'; break; } # we want to know later which table was selected $new_key = $table . '.' . $key; # give the user a hint about the data type of the column $new_fields[$new_key] = strtoupper($table) . ': ' . $key . ' - ' . $value; } } cacti_log($function . ' returns: ' . cacti_sizeof($new_fields), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); return $new_fields; } /* * get_field_names * @arg $snmp_query_id snmp query id * return all field names for that snmp query, taken from snmp_cache */ function get_field_names($snmp_query_id) { $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . " called: $snmp_query_id", false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* get the unique field values from the database */ $sql = 'SELECT DISTINCT field_name FROM host_snmp_cache WHERE snmp_query_id=' . $snmp_query_id; $fields = db_fetch_assoc($sql); cacti_log($function . ' returns: ' . cacti_sizeof($fields), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); return $fields; } function array_to_list($array, $sql_column) { $function = automation_function_with_pid(__FUNCTION__); /* if the last item is null; pop it off */ $counter = cacti_count($array); if (empty($array[$counter-1]) && $counter > 1) { array_pop($array); $counter = cacti_count($array); } if ($counter > 0) { $sql = '('; for ($i=0; $i<$counter; $i++) { $sql .= $array[$i][$sql_column]; if ($i+1 < $counter) { $sql .= ','; } } $sql .= ')'; cacti_log($function . "() returns: $sql", false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); return $sql; } } function array_minus($big_array, $small_array) { # remove all unwanted fields if (cacti_sizeof($small_array)) { foreach($small_array as $exclude) { if (array_key_exists($exclude, $big_array)) { unset($big_array[$exclude]); } } } return $big_array; } function automation_string_replace($search, $replace, $target) { $repl = preg_replace('/' . $search . '/i', $replace, $target); return preg_split('/\\\\n/', $repl, -1, PREG_SPLIT_NO_EMPTY); } function global_item_edit($rule_id, $rule_item_id, $rule_type) { global $config, $fields_automation_match_rule_item_edit, $fields_automation_graph_rule_item_edit; global $fields_automation_tree_rule_item_edit, $automation_tree_header_types; global $automation_op_array; switch ($rule_type) { case AUTOMATION_RULE_TYPE_GRAPH_MATCH: // Graph Rules - Device Selection Criteria > Edit $title = __('Device Match Rule'); $item_table = 'automation_match_rule_items'; $sql_and = ' AND rule_type=' . $rule_type; $tables = array ('host', 'host_templates'); $automation_rule = db_fetch_row_prepared('SELECT * FROM automation_graph_rules WHERE id = ?', array($rule_id)); $_fields_rule_item_edit = $fields_automation_match_rule_item_edit; $query_fields = get_query_fields('host_template', array('id', 'hash')); $query_fields += get_query_fields('host', array('id', 'host_template_id')); $_fields_rule_item_edit['field']['array'] = $query_fields; $module = 'automation_graph_rules.php'; break; case AUTOMATION_RULE_TYPE_GRAPH_ACTION: // Graph Rules - Graph Creation Criterial > Edit $title = __('Create Graph Rule'); $tables = array(AUTOMATION_RULE_TABLE_XML); $item_table = 'automation_graph_rule_items'; $sql_and = ''; $automation_rule = db_fetch_row_prepared('SELECT * FROM automation_graph_rules WHERE id = ?', array($rule_id)); $_fields_rule_item_edit = $fields_automation_graph_rule_item_edit; $xml_array = get_data_query_array($automation_rule['snmp_query_id']); $fields = array(); if (cacti_sizeof($xml_array) && cacti_sizeof($xml_array['fields'])) { foreach($xml_array['fields'] as $key => $value) { # ... work on all input fields if (isset($value['direction']) && ($value['direction'] == 'input' || $value['direction'] == 'input-output')) { $fields[$key] = $key . ' - ' . $value['name']; } } $_fields_rule_item_edit['field']['array'] = $fields; } $module = 'automation_graph_rules.php'; break; case AUTOMATION_RULE_TYPE_TREE_MATCH: // Tree Rules - Object Selection > Edit $item_table = 'automation_match_rule_items'; $sql_and = ' AND rule_type=' . $rule_type; $automation_rule = db_fetch_row_prepared('SELECT * FROM automation_tree_rules WHERE id = ?', array($rule_id)); $_fields_rule_item_edit = $fields_automation_match_rule_item_edit; $query_fields = get_query_fields('host_template', array('id', 'hash')); $query_fields += get_query_fields('host', array('id', 'host_template_id')); if ($automation_rule['leaf_type'] == TREE_ITEM_TYPE_HOST) { $title = __('Device Match Rule'); $tables = array ('host', 'host_templates'); } elseif ($automation_rule['leaf_type'] == TREE_ITEM_TYPE_GRAPH) { $title = __('Graph Match Rule'); $tables = array ('host', 'host_templates'); # add some more filter columns for a GRAPH match $query_fields += get_query_fields('graph_templates', array('id', 'hash')); $query_fields += array('gtg.title' => 'GTG: title - varchar(255)'); $query_fields += array('gtg.title_cache' => 'GTG: title_cache - varchar(255)'); } $_fields_rule_item_edit['field']['array'] = $query_fields; $module = 'automation_tree_rules.php'; break; case AUTOMATION_RULE_TYPE_TREE_ACTION: // Tree Rules - Tree Creation Criteria > Edit $item_table = 'automation_tree_rule_items'; $sql_and = ''; $automation_rule = db_fetch_row_prepared('SELECT * FROM automation_tree_rules WHERE id = ?', array($rule_id)); $_fields_rule_item_edit = $fields_automation_tree_rule_item_edit; $query_fields = get_query_fields('host_template', array('id', 'hash')); $query_fields += get_query_fields('host', array('id', 'host_template_id')); /* list of allowed header types depends on rule leaf_type * e.g. for a Device Rule, only Device-related header types make sense */ if ($automation_rule['leaf_type'] == TREE_ITEM_TYPE_HOST) { $title = __('Create Tree Rule (Device)'); $tables = array ('host', 'host_templates'); } elseif ($automation_rule['leaf_type'] == TREE_ITEM_TYPE_GRAPH) { $title = __('Create Tree Rule (Graph)'); $tables = array ('host', 'host_templates'); # add some more filter columns for a GRAPH match $query_fields += get_query_fields('graph_templates', array('id', 'hash')); $query_fields += array('gtg.title' => 'GTG: title - varchar(255)'); $query_fields += array('gtg.title_cache' => 'GTG: title_cache - varchar(255)'); } $_fields_rule_item_edit['field']['array'] = $query_fields; $module = 'automation_tree_rules.php'; break; } if (!empty($rule_item_id)) { $automation_item = db_fetch_row_prepared("SELECT * FROM $item_table WHERE id = ? $sql_and", array($rule_item_id)); if (cacti_sizeof($automation_item)) { $missing_key = $automation_item['field']; if (empty($missing_key)) { // Fixed String } elseif (!array_key_exists($missing_key, $_fields_rule_item_edit['field']['array'])) { $missing_array = explode('.',$missing_key); if (cacti_sizeof($missing_array) > 1) { $missing_table = strtoupper($missing_array[0]); $missing_value = strtolower($missing_array[1]); } else { $missing_table = ''; $missing_value = strtolower($missing_array[0]); } $_fields_rule_item_edit['field']['array'] = array_merge( array($automation_item['field'] => 'Unknown: ' . $missing_table . ': ' . $missing_value), $_fields_rule_item_edit['field']['array'] ); } } $header_label = __esc('Rule Item [edit rule item for %s: %s]', $title, $automation_rule['name']); } else { $header_label = __esc('Rule Item [new rule item for %s: %s]', $title, $automation_rule['name']); $automation_item = array(); $automation_item['sequence'] = get_sequence('', 'sequence', $item_table, 'rule_id=' . $rule_id . $sql_and); } form_start($module, 'form_automation_global_item_edit'); html_start_box($header_label, '100%', true, '3', 'center', ''); draw_edit_form( array( 'config' => array('no_form_tag' => true), 'fields' => inject_form_variables($_fields_rule_item_edit, (isset($automation_item) ? $automation_item : array()), (isset($automation_rule) ? $automation_rule : array())) ) ); html_end_box(true, true); } /** * hook executed for a graph template * @param $host_id - the host to perform automation on * @param $graph_template_id - the graph_template_id to perform automation on */ function automation_hook_graph_template($host_id, $graph_template_id) { global $config; $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . ' called: Device[' . $host_id . '], GT[' . $graph_template_id . ']', false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); if (read_config_option('automation_graphs_enabled') == '') { cacti_log($function . ' Device[' . $host_id . '] - skipped: Graph Creation Switch is: ' . (read_config_option('automation_graphs_enabled') == '' ? 'off' : 'on'), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); return; } automation_execute_graph_template($host_id, $graph_template_id); } /** * hook executed for a new graph on a tree * @param $data - data passed from hook */ function automation_hook_graph_create_tree($data) { global $config; $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . ' called: ' . json_encode($data), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); if (read_config_option('automation_tree_enabled') == '') { cacti_log($function. ' skipped: Tree Creation Switch is: ' . (read_config_option('automation_tree_enabled') == '' ? 'off' : 'on'), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); return; } automation_execute_graph_create_tree($data['id']); /* make sure, the next plugin gets required $data */ return($data); } /** * run rules for a data query * @param $data - data passed from hook */ function automation_execute_data_query($host_id, $snmp_query_id) { global $config; $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . ' Device[' . $host_id . "] - start - data query: $snmp_query_id", false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); # get all related rules for that data query that are enabled $sql = 'SELECT agr.id, agr.name, agr.snmp_query_id, agr.graph_type_id FROM automation_graph_rules AS agr INNER JOIN host_snmp_query AS hsq ON agr.snmp_query_id = hsq.snmp_query_id WHERE agr.snmp_query_id = ? AND hsq.host_id = ? AND enabled="on"'; $rules = db_fetch_assoc_prepared($sql, array($snmp_query_id, $host_id)); cacti_log($function . ' Device[' . $host_id . '] - sql: ' . str_replace("\t", '', str_replace("\n", ' ', $sql)) . ' - found: ' . cacti_sizeof($rules), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_DEBUG); if (!cacti_sizeof($rules)) { return; } # now walk all rules and create graphs if (cacti_sizeof($rules)) { foreach ($rules as $rule) { cacti_log($function . ' Device[' . $host_id . '] - rule=' . $rule['id'] . ' name: ' . $rule['name'], false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* build magic query, for matching hosts JOIN tables host and host_template */ $sql_query = 'SELECT h.id AS host_id, h.hostname, h.description, ht.name AS host_template_name FROM host AS h LEFT JOIN host_template AS ht ON h.host_template_id = ht.id'; /* get the WHERE clause for matching hosts */ $sql_filter = build_matching_objects_filter($rule['id'], AUTOMATION_RULE_TYPE_GRAPH_MATCH); /* now we build up a new query for counting the rows */ $rows_query = $sql_query . ' WHERE (' . $sql_filter . ') AND h.id=' . $host_id . ' AND h.deleted = ""'; $hosts = db_fetch_assoc($rows_query, false); cacti_log($function . ' Device[' . $host_id . '] - create sql: ' . str_replace("\n",' ', $rows_query) . ' matches: ' . cacti_sizeof($hosts), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_DEBUG); if (!cacti_sizeof($hosts)) { continue; } create_dq_graphs($host_id, $snmp_query_id, $rule); } } } /** * automation_graph_automation_eligible * * This function determines if a Graph Template is eligible for * automation. If there are any of the following that have * designated allowing for an over-ride, but to not have a default * value, then the Graph Template is not eligible for automatic * automation. * * Data Input Fields * Data Template Data Fields * Graph Template Fields * * @param $graph_template_id * * @return boolean eligibility */ function automation_graph_automation_eligible($graph_template_id) { $graph_template = db_fetch_row_prepared('SELECT * FROM graph_templates_graph WHERE graph_template_id = ? AND local_graph_id = 0', array($graph_template_id)); // Check the Graph Template first for adherence if (cacti_sizeof($graph_template)) { foreach($graph_template as $field => $value) { if (substr($field, 0, 2) == 't_') { $parent = substr($field, 2); if (isset($graph_template[$parent])) { if ($value == 'on' && $graph_template[$parent] == '') { return false; } } } } } // Next let's check it's source Data Templates $data_templates = db_fetch_assoc_prepared('SELECT DISTINCT dtd.* FROM data_template_data AS dtd INNER JOIN data_template_rrd AS dtr ON dtd.data_template_id = dtr.data_template_id INNER JOIN graph_templates_item AS gti ON dtr.id = gti.task_item_id WHERE gti.graph_template_id = ? AND dtd.local_data_id = 0 AND dtr.local_data_id = 0 AND gti.hash != ""', array($graph_template_id)); if (cacti_sizeof($data_templates)) { foreach($data_templates as $dtd) { foreach($dtd as $field => $value) { if (substr($field, 0, 2) == 't_') { $parent = substr($field, 2); if (isset($dtd[$parent])) { if ($value == 'on' && $dtd[$parent] == '') { return false; } } } } // Lastly check the data input fields $input_fields = db_fetch_assoc_prepared('SELECT dif.data_input_id, did.t_value, did.value, dtd.name FROM data_template_data AS dtd INNER JOIN data_template AS dt ON dt.id = dtd.data_template_id INNER JOIN data_input_data AS did ON did.data_template_data_id = dtd.id INNER JOIN data_input_fields AS dif ON dif.id = did.data_input_field_id WHERE dt.hash != "" AND dtd.id = ? AND dtd.local_data_id = 0 AND dif.input_output = "in" AND dif.type_code = "" AND did.t_value = "on" AND did.value = ""', array($dtd['id'])); if (cacti_sizeof($input_fields)) { return false; } } } return true; } /** * run rules for a graph template * @param $data - data passed from hook */ function automation_execute_graph_template($host_id, $graph_template_id) { global $config; include_once($config['base_path'] . '/lib/template.php'); include_once($config['base_path'] . '/lib/api_automation_tools.php'); include_once($config['base_path'] . '/lib/utility.php'); $dataSourceId = ''; $returnArray = array(); $suggested_values = array(); $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . ' called: Device[' . $host_id . '] - GT[' . $graph_template_id . ']', false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); # are there any input fields? if so use the default values if ($graph_template_id > 0) { $input_fields = getInputFields($graph_template_id); if (cacti_sizeof($input_fields)) { $suggested_vals[$graph_template_id]['custom_data'] = array(); foreach($input_fields as $field) { $suggested_vals[$graph_template_id]['custom_data'][$field['data_template_id']][$field['data_input_field_id']] = $field['default']; } } } # graph already present? $existsAlready = db_fetch_cell_prepared('SELECT id FROM graph_local WHERE graph_template_id = ? AND host_id = ?', array($graph_template_id, $host_id)); if ($existsAlready > 0) { $dataSourceId = db_fetch_cell_prepared('SELECT data_template_rrd.local_data_id FROM graph_templates_item, data_template_rrd WHERE graph_templates_item.local_graph_id = ? AND graph_templates_item.task_item_id = data_template_rrd.id LIMIT 1', array($existsAlready)); cacti_log('NOTE: ' . $function . ' Device[' . $host_id . "] Graph Creation Skipped - Already Exists - Graph[$existsAlready] - DS[$dataSourceId]", false, 'AUTOM8', POLLER_VERBOSITY_MEDIUM); return; } elseif (automation_graph_automation_eligible($graph_template_id)) { if (test_data_sources($graph_template_id, $host_id)) { cacti_log('NOTE: Data Check Succeeded for - Device[' . $host_id . '], GT[' . $graph_template_id . ']', false, 'AUTOM8'); $returnArray = create_complete_graph_from_template($graph_template_id, $host_id, array(), $suggested_values); $dataSourceId = ''; if ($returnArray !== false) { if (cacti_sizeof($returnArray)) { if (isset($returnArray['local_data_id'])) { foreach($returnArray['local_data_id'] as $item) { push_out_host($host_id, $item); if ($dataSourceId != '') { $dataSourceId .= ', ' . $item; } else { $dataSourceId = $item; } } cacti_log('NOTE: Graph Added - Device[' . $host_id . '], Graph[' . $returnArray['local_graph_id'] . "], DS[$dataSourceId]", false, 'AUTOM8'); } } else { cacti_log('ERROR: Device[' . $host_id . '], GT[' . $graph_template_id . '] Graph not added due to missing data sources.', false, 'AUTOM8'); } } else { cacti_log('ERROR: Device[' . $host_id . '], GT[' . $graph_template_id . '] Graph not added due to whitelist check failure.', false, 'AUTOM8'); } } else { cacti_log('NOTE: Device[' . $host_id . '], GT[' . $graph_template_id . '] Graph not added due to invalid data source output.', false, 'AUTOM8'); } } else { cacti_log('NOTE: Device[' . $host_id . '], GT[' . $graph_template_id . '] Graph not added due to no default value for overridable field.', false, 'AUTOM8'); } } /** * run rules for a new device in a tree * @param $host_id - the host id of the device */ function automation_execute_device_create_tree($host_id) { global $config; /* the $data array holds all information about the host we're just working on * even if we selected multiple hosts, the calling code will scan through the list * so we only have a single host here */ $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . " Device[$host_id] called", false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* * find all active Tree Rules * checking whether a specific rule matches the selected host * has to be done later */ $sql = "SELECT atr.id, atr.name, atr.tree_id, atr.tree_item_id, atr.leaf_type, atr.host_grouping_type FROM automation_tree_rules AS atr WHERE enabled='on' AND leaf_type=" . TREE_ITEM_TYPE_HOST; $rules = db_fetch_assoc($sql); cacti_log($function . ' Device[' . $host_id . '], matching rule sql: ' . str_replace("\n",'',$sql) . ' matches: ' . cacti_sizeof($rules), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_DEBUG); /* now walk all rules */ if (cacti_sizeof($rules)) { foreach ($rules as $rule) { cacti_log($function . " Device[$host_id], rule: " . $rule['id'] . ' name: ' . $rule['name'] . ' type: ' . $rule['leaf_type'], false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* does the rule apply to the current host? * test 'eligible objects' rule items */ $matches = get_matching_hosts($rule, AUTOMATION_RULE_TYPE_TREE_MATCH, 'h.id=' . $host_id); cacti_log($function . " Device[$host_id], rule: " . $rule['id'] . ', matching hosts: ' . json_encode($matches), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* if the rule produces a match, we will have to create all required tree nodes */ if (cacti_sizeof($matches)) { /* create the bunch of header nodes */ $parent = create_all_header_nodes($host_id, $rule); cacti_log($function . " Device[$host_id], rule: " . $rule['id'] . ', parent: ' . $parent, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* now that all rule items have been executed, add the item itself */ $node = create_device_node($host_id, $parent, $rule); cacti_log($function . " Device[$host_id], rule: " . $rule['id'] . ', node: ' . $node, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); } } } } /** * run rules for a new graph on a tree * @param $data - data passed from hook */ function automation_execute_graph_create_tree($graph_id) { global $config; /* the $data array holds all information about the graph we're just working on * even if we selected multiple graphs, the calling code will scan through the list * so we only have a single graph here */ $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . ' Graph[' . $graph_id . '] called', false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* * find all active Tree Rules * checking whether a specific rule matches the selected graph * has to be done later */ $sql = "SELECT atr.id, atr.name, atr.tree_id, atr.tree_item_id, atr.leaf_type, atr.host_grouping_type FROM automation_tree_rules AS atr WHERE enabled='on' AND leaf_type=" . TREE_ITEM_TYPE_GRAPH; $rules = db_fetch_assoc($sql); cacti_log($function . ' Graph[' . $graph_id . '], Matching rule sql: ' . str_replace("\n",' ', $sql) . ' matches: ' . cacti_sizeof($rules), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_DEBUG); /* now walk all rules */ if (cacti_sizeof($rules)) { foreach ($rules as $rule) { cacti_log($function . ' Graph[' . $graph_id . '], rule: ' . $rule['id'] . ', name: ' . $rule['name'] . ', type: ' . $rule['leaf_type'], false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* does this rule apply to the current graph? * test 'eligible objects' rule items */ $matches = get_matching_graphs($rule, AUTOMATION_RULE_TYPE_TREE_MATCH, 'gl.id=' . $graph_id); cacti_log($function . ' Graph[' . $graph_id . '], rule: ' . $rule['id'] . ', matching graphs: ' . json_encode($matches), false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* if the rule produces a match, we will have to create all required tree nodes */ if (cacti_sizeof($matches)) { /* create the bunch of header nodes */ $parent = create_all_header_nodes($graph_id, $rule); cacti_log($function . ' Graph[' . $graph_id . '], Rule: ' . $rule['id'] . ', Parent: ' . $parent, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* now that all rule items have been executed, add the item itself */ $node = create_graph_node($graph_id, $parent, $rule); cacti_log($function . ' Graph[' . $graph_id . '], Rule: ' . $rule['id'] . ', Node: ' . $node, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); } } } } /** * create all graphs for a data query * @param int $host_id - host id * @param int $snmp_query_id - snmp query id * @param array $rule - matching rule */ function create_dq_graphs($host_id, $snmp_query_id, $rule) { global $config, $automation_op_array, $automation_oper; $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . ' Device[' . $host_id . "] - snmp query: $snmp_query_id - rule: " . $rule['name'], false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); $snmp_query_array = array(); $snmp_query_array['snmp_query_id'] = $rule['snmp_query_id']; $snmp_query_array['snmp_index_on'] = get_best_data_query_index_type($host_id, $rule['snmp_query_id']); $snmp_query_array['snmp_query_graph_id'] = $rule['graph_type_id']; # get all rule items $automation_rule_items = db_fetch_assoc_prepared('SELECT * FROM automation_graph_rule_items AS agri WHERE rule_id = ? ORDER BY sequence', array($rule['id'])); $automation_rule_fields = array_rekey( db_fetch_assoc_prepared('SELECT field FROM automation_graph_rule_items AS agri WHERE field != "" AND rule_id = ?', array($rule['id'])), 'field', 'field' ); # and all matching snmp_indices from snmp_cache $rule_name = db_fetch_cell_prepared('SELECT name FROM automation_graph_rules WHERE id = ?', array($rule['id'])); /* get the unique field values from the database */ $field_names = array_rekey( db_fetch_assoc_prepared('SELECT DISTINCT field_name FROM host_snmp_cache AS hsc WHERE snmp_query_id= ? AND host_id = ?', array($snmp_query_id, $host_id)), 'field_name', 'field_name' ); /* build magic query */ $sql_query = 'SELECT host_id, snmp_query_id, snmp_index'; /* check for possible SQL errors */ foreach($automation_rule_fields as $column) { if (array_search($column, $field_names) === false) { cacti_log('WARNING: Automation Rule[' . $rule_name . '] for Device[' . $host_id . '] - DQ[' . $snmp_query_id . '] includes a SQL column ' . $column . ' that is not found for the Device. Can not continue.', false, 'AUTOM8'); return false; } } $num_visible_fields = cacti_sizeof($field_names); $i = 0; if (cacti_sizeof($field_names) > 0) { foreach($field_names as $column) { $sql_query .= ", MAX(CASE WHEN field_name ='$column' THEN field_value ELSE NULL END) AS '$column'"; $i++; } } $sql_query .= ' FROM host_snmp_cache AS hsc WHERE snmp_query_id=' . $snmp_query_id . ' AND host_id=' . $host_id . ' GROUP BY snmp_query_id, snmp_index'; $sql_filter = build_rule_item_filter($automation_rule_items, ' a.'); if (strlen($sql_filter)) { $sql_filter = ' WHERE' . $sql_filter; } /* add the additional filter settings to the original data query. IMO it's better for the MySQL server to use the original one as an subquery which requires MySQL v4.1(?) or higher */ $sql_query = 'SELECT * FROM (' . $sql_query . ") as a $sql_filter"; /* fetch snmp indices */ # print $sql_query . '\n'; $snmp_query_indexes = db_fetch_assoc($sql_query); # now create the graphs if (cacti_sizeof($snmp_query_indexes)) { $graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id FROM snmp_query_graph WHERE id = ?', array($rule['graph_type_id'])); cacti_log($function . ' Found Template for Device[' . $host_id . '] - GT[' . $graph_template_id . ']', false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); foreach ($snmp_query_indexes as $snmp_index) { $snmp_query_array['snmp_index'] = $snmp_index['snmp_index']; cacti_log($function . ' Device[' . $host_id . '] - checking index: ' . $snmp_index['snmp_index'], false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); $existsAlready = db_fetch_cell_prepared('SELECT DISTINCT gl.id FROM graph_local AS gl WHERE gl.snmp_query_graph_id = ? AND gl.host_id = ? AND gl.snmp_query_id = ? AND gl.snmp_index = ?', array($rule['graph_type_id'], $host_id, $rule['snmp_query_id'], $snmp_query_array['snmp_index'])); if (isset($existsAlready) && $existsAlready > 0) { cacti_log('NOTE: ' . $function . ' Device[' . $host_id . "] Graph Creation Skipped - Already Exists - Graph[$existsAlready]", false, 'AUTOM8', POLLER_VERBOSITY_HIGH); continue; } $suggested_values = array(); if (test_data_sources($graph_template_id, $host_id, $rule['snmp_query_id'], $snmp_query_array['snmp_index'])) { $return_array = create_complete_graph_from_template($graph_template_id, $host_id, $snmp_query_array, $suggested_values); if ($return_array !== false) { if (cacti_sizeof($return_array) && array_key_exists('local_graph_id', $return_array) && array_key_exists('local_data_id', $return_array)) { $data_source_id = db_fetch_cell_prepared('SELECT data_template_rrd.local_data_id FROM graph_templates_item, data_template_rrd WHERE graph_templates_item.local_graph_id = ? AND graph_templates_item.task_item_id = data_template_rrd.id LIMIT 1', array($return_array['local_graph_id'])); foreach($return_array['local_data_id'] as $item) { push_out_host($host_id, $item); if ($data_source_id != '') { $data_source_id .= ', ' . $item; } else { $data_source_id = $item; } } cacti_log('NOTE: Graph Added - Device[' . $host_id . '], Graph[' . $return_array['local_graph_id'] . "], DS[$data_source_id], Rule[" . $rule['id'] . ']', false, 'AUTOM8'); } else { cacti_log('ERROR: Device[' . $host_id . '], GT[' . $graph_template_id . '], DQ[' . $rule['snmp_query_id'] . '], Index[' . $snmp_query_array['snmp_index'] . '], Rule[' . $rule['id'] . '] Graph not added due to missing data sources.', false, 'AUTOM8'); } } else { cacti_log('ERROR: Device[' . $host_id . '], GT[' . $graph_template_id . '], DQ[' . $rule['snmp_query_id'] . '], Index[' . $snmp_query_array['snmp_index'] . '], Rule[' . $rule['id'] . '] Graph not added due to whitelist failure.', false, 'AUTOM8'); } } else { cacti_log('NOTE: Device[' . $host_id . '], GT[' . $graph_template_id . '], DQ[' . $rule['snmp_query_id'] . '], Index[' . $snmp_query_array['snmp_index'] . '], Rule[' . $rule['id'] . '] Graph not added due to invalid data returned.', false, 'AUTOM8'); } } } } /** * create_all_header_nodes - walk across all tree rule items * - get all related rule items * - take header type into account * - create (multiple) header nodes * * @arg $item_id id of the host/graph we're working on * @arg $rule the rule we're working on * returns the last tree item that was hooked into the tree */ function create_all_header_nodes($item_id, $rule) { global $config, $automation_tree_header_types; # get all related rules that are enabled $tree_items = db_fetch_assoc_prepared('SELECT * FROM automation_tree_rule_items AS atri WHERE atri.rule_id = ? ORDER BY sequence', array($rule['id'])); $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . " called: Item $item_id matches: " . cacti_sizeof($tree_items) . ' items', false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); /* start at the given tree item * it may be worth verifying existence of this entry * in case it was selected once but then deleted */ $parent_tree_item_id = $rule['tree_item_id']; # now walk all rules and create tree nodes if (cacti_sizeof($tree_items)) { /* build magic query, * for matching hosts JOIN tables host and host_template */ if ($rule['leaf_type'] == TREE_ITEM_TYPE_HOST) { $sql_tables = 'FROM host AS h LEFT JOIN host_template AS ht ON h.host_template_id=ht.id '; $sql_where = 'WHERE h.id='. $item_id . ' AND h.deleted = "" '; } elseif ($rule['leaf_type'] == TREE_ITEM_TYPE_GRAPH) { /* graphs require a different set of tables to be joined */ $sql_tables = 'FROM host AS h LEFT JOIN host_template AS ht ON h.host_template_id=ht.id LEFT JOIN graph_local AS gl ON h.id=gl.host_id LEFT JOIN graph_templates AS gt ON gl.graph_template_id=gt.id LEFT JOIN graph_templates_graph AS gtg ON gl.id=gtg.local_graph_id '; $sql_where = 'WHERE gl.id=' . $item_id . ' AND h.deleted = "" '; } /* get the WHERE clause for matching hosts */ $sql_filter = build_matching_objects_filter($rule['id'], AUTOMATION_RULE_TYPE_TREE_MATCH); foreach ($tree_items as $tree_item) { if ($tree_item['field'] === AUTOMATION_TREE_ITEM_TYPE_STRING) { # for a fixed string, use the given text $sql = ''; $target = $automation_tree_header_types[AUTOMATION_TREE_ITEM_TYPE_STRING]; } else { $sql_field = $tree_item['field'] . ' AS source '; /* now we build up a new query for counting the rows */ $sql = 'SELECT ' . $sql_field . $sql_tables . $sql_where . ' AND (' . $sql_filter . ')'; $target = db_fetch_cell($sql, '', false); } cacti_log($function . ' Item ' . $item_id . ' - sql: ' . str_replace("\m",'',$sql) . ' matches: ' . $target, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_DEBUG); $parent_tree_item_id = create_multi_header_node($target, $rule, $tree_item, $parent_tree_item_id); } } return $parent_tree_item_id; } /** * create_multi_header_node - work on a single header item * - evaluate replacement rule * - this may return an array of new header items * - walk that array to create all header items for this single rule item * * @arg $target string (name) of the object; e.g. ht.name * @arg $rule rule * @arg $tree_item rule item; replacement_pattern may result in multi-line replacement * @arg $parent_tree_item_id parent tree item id * * *return id of the header that was hooked in */ function create_multi_header_node($object, $rule, $tree_item, $parent_tree_item_id){ global $config; $function = automation_function_with_pid(__FUNCTION__); cacti_log($function . " - object: '" . $object . "', Header: '" . $tree_item['search_pattern'] . "', parent: " . $parent_tree_item_id, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); if ($tree_item['field'] === AUTOMATION_TREE_ITEM_TYPE_STRING) { $parent_tree_item_id = create_header_node($tree_item['search_pattern'], $rule, $tree_item, $parent_tree_item_id); cacti_log($function . " called - object: '" . $object . "', Header: '" . $tree_item['search_pattern'] . "', hooked at: " . $parent_tree_item_id, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); } else { $replacement = automation_string_replace($tree_item['search_pattern'], $tree_item['replace_pattern'], $object); /* build multiline
'; print_r($replacement); print ''; for ($j=0; cacti_sizeof($replacement); $j++) { $title = array_shift($replacement); $parent_tree_item_id = create_header_node($title, $rule, $tree_item, $parent_tree_item_id); cacti_log($function . " - object: '" . $object . "', Header: '" . $title . "', hooked at: " . $parent_tree_item_id, false, 'AUTOM8 TRACE', POLLER_VERBOSITY_HIGH); } } return $parent_tree_item_id; } /** * create a single tree header node * * @param string $title - graph title * @param array $rule - rule * @param array $item - item * @param int $parent_tree_item_id - parent item id * * @return int - id of new item */ function create_header_node($title, $rule, $item, $parent_tree_item_id) { global $config; $id = 0; # create a new entry $local_graph_id = 0; # headers don't need no graph_id $host_id = 0; # or a host_id $site_id = 0; # or a site_id $propagate = ($item['propagate_changes'] != ''); $function = automation_function_with_pid(__FUNCTION__); if (api_tree_branch_exists($rule['tree_id'], $parent_tree_item_id, $title)) { $new_item = api_tree_get_branch_id($rule['tree_id'], $parent_tree_item_id, $title); cacti_log('NOTE: ' . $function . ' Parent[' . $parent_tree_item_id . '] Tree Item - Already Exists', false, 'AUTOM8', POLLER_VERBOSITY_MEDIUM); } else { $new_item = api_tree_item_save($id, $rule['tree_id'], TREE_ITEM_TYPE_HEADER, $parent_tree_item_id, $title, $local_graph_id, $host_id, $site_id, $rule['host_grouping_type'], $item['sort_type'], $propagate); if (isset($new_item) && $new_item > 0) { cacti_log('NOTE: ' . $function . ' Parent[' . $parent_tree_item_id . '] Tree Item - Added - id: (' . $new_item . ') Title: (' .$title . ')', false, 'AUTOM8'); } else { cacti_log('WARNING: ' . $function . ' Parent[' . $parent_tree_item_id . '] Tree Item - Not Added', false, 'AUTOM8'); } } return $new_item; } /** * add a device to the tree * @param int $host_id - host id * @param int $parent - parent id * @param array $rule - rule * @return int - id of new item */ function create_device_node($host_id, $parent, $rule) { global $config; $id = 0; # create a new entry $local_graph_id = 0; # hosts don't need no graph_id $site_id = 0; # hosts don't need no site_id $title = ''; # nor a title $sort_type = 0; # nor a sort type $propagate = false; # nor a propagation flag $function = automation_function_with_pid(__FUNCTION__); if (api_tree_host_exists($rule['tree_id'], $parent, $host_id)) { $new_item = db_fetch_cell_prepared('SELECT id FROM graph_tree_items WHERE host_id = ? AND parent = ? AND graph_tree_id = ?', array($host_id, $parent, $rule['tree_id'])); cacti_log('NOTE: ' . $function . ' Device[' . $host_id . '] Tree Item - Already Exists', false, 'AUTOM8', POLLER_VERBOSITY_MEDIUM); } else { $new_item = api_tree_item_save($id, $rule['tree_id'], TREE_ITEM_TYPE_HOST, $parent, $title, $local_graph_id, $host_id, $site_id, $rule['host_grouping_type'], $sort_type, $propagate); if (isset($new_item) && $new_item > 0) { cacti_log('NOTE: ' . $function . ' Device[' . $host_id . '] Tree Item - Added - Parent[' . $parent . '] Id[' . $new_item . ']', false, 'AUTOM8'); } else { cacti_log('WARNING: ' . $function . ' Device[' . $host_id . '] Tree Item - Not Added', false, 'AUTOM8'); } } return $new_item; } /** * add a site to the tree * @param int $site_id - site id * @param int $parent - parent id * @param array $rule - rule * @return int - id of new item */ function create_site_node($site_id, $parent, $rule) { global $config; $id = 0; # create a new entry $local_graph_id = 0; # hosts don't need no graph_id $host_id = 0; # hosts don't need no host_id $title = ''; # nor a title $sort_type = 0; # nor a sort type $propagate = false; # nor a propagation flag $function = 'Function[' . __FUNCTION__ . ']'; if (api_tree_site_exists($rule['tree_id'], $parent, $site_id)) { $new_item = db_fetch_cell_prepared('SELECT id FROM graph_tree_items WHERE site_id = ? AND parent = ? AND graph_tree_id = ?', array($site_id, $parent, $rule['tree_id'])); cacti_log('NOTE: ' . $function . ' Site[' . $host_id . '] Tree Item - Already Exists', false, 'AUTOM8', POLLER_VERBOSITY_MEDIUM); } else { $new_item = api_tree_item_save($id, $rule['tree_id'], TREE_ITEM_TYPE_HOST, $parent, $title, $local_graph_id, $host_id, $site_id, $rule['host_grouping_type'], $sort_type, $propagate); if (isset($new_item) && $new_item > 0) { cacti_log('NOTE: ' . $function . ' Site[' . $site_id . '] Tree Item - Added - id: (' . $new_item . ')', false, 'AUTOM8'); } else { cacti_log('WARNING: ' . $function . ' Site[' . $site_id . '] Tree Item - Not Added', false, 'AUTOM8'); } } return $new_item; } /** * add a device to the tree * @param int $graph_id - graph id * @param int $parent - parent id * @param array $rule - rule * @return int - id of new item */ function create_graph_node($graph_id, $parent, $rule) { global $config; $id = 0; # create a new entry $host_id = 0; # graphs don't need no host_id $site_id = 0; # graphs don't need no site_id $title = ''; # nor a title $sort_type = 0; # nor a sort type $propagate = false; # nor a propagation flag $function = automation_function_with_pid(__FUNCTION__); if (api_tree_graph_exists($rule['tree_id'], $parent, $graph_id)) { $new_item = db_fetch_cell_prepared('SELECT id FROM graph_tree_items WHERE local_graph_id = ? AND parent = ? AND graph_tree_id = ?', array($graph_id, $parent, $rule['tree_id'])); cacti_log('NOTE: ' . $function . ' Graph[' . $graph_id . '] Tree Item - Already Exists', false, 'AUTOM8', POLLER_VERBOSITY_MEDIUM); } else { $new_item = api_tree_item_save($id, $rule['tree_id'], TREE_ITEM_TYPE_GRAPH, $parent, $title, $graph_id, $host_id, $site_id, $rule['host_grouping_type'], $sort_type, $propagate); if (isset($new_item) && $new_item > 0) { cacti_log('NOTE: ' . $function . ' Graph[' . $graph_id . '] Tree Item - Added - id: (' . $new_item . ')', false, 'AUTOM8'); } else { cacti_log('WARNING: ' . $function . ' Graph[' . $graph_id . '] Tree Item - Not Added', false, 'AUTOM8'); } } return $new_item; } function automation_poller_bottom() { global $config; $command_string = cacti_escapeshellcmd(read_config_option('path_php_binary')); // If its not set, just assume its in the path if (trim($command_string) == '') { $command_string = 'php'; } $extra_args = ' -q ' . cacti_escapeshellarg($config['base_path'] . '/poller_automation.php') . ' -M'; exec_background($command_string, $extra_args); } function automation_add_device($device, $web = false) { global $plugins, $config; $template_id = $device['host_template']; $snmp_sysName = $device['snmp_sysName']; $description = ($snmp_sysName != '' ? $snmp_sysName : ($device['hostname'] == '' ? $device['ip'] : $device['hostname'])); $poller_id = isset($device['poller_id']) ? $device['poller_id'] : read_config_option('default_poller'); $site_id = isset($device['site_id']) ? $device['site_id'] : read_config_option('default_site'); $ip = isset($device['ip']) ? $device['ip']:$device['ip_address']; $snmp_community = $device['snmp_community']; $snmp_ver = $device['snmp_version']; $snmp_username = $device['snmp_username']; $snmp_password = $device['snmp_password']; $snmp_port = $device['snmp_port']; $snmp_timeout = isset($device['snmp_timeout']) ? $device['snmp_timeout']:read_config_option('snmp_timeout'); $disable = ''; $availability_method = isset($device['availability_method']) ? $device['availability_method']:read_config_option('availability_method'); $ping_method = isset($device['ping_method']) ? $device['ping_method'] : read_config_option('ping_method'); $ping_port = isset($device['ping_port']) ? $device['ping_port'] : read_config_option('ping_port'); $ping_timeout = isset($device['ping_timeout']) ? $device['ping_timeout'] : read_config_option('ping_timeout'); $ping_retries = isset($device['ping_retries']) ? $device['ping_retries'] : read_config_option('ping_retries'); $notes = isset($device['notes']) ? $device['notes'] : __('Added by Cacti Automation'); $snmp_auth_protocol = $device['snmp_auth_protocol']; $snmp_priv_passphrase = $device['snmp_priv_passphrase']; $snmp_priv_protocol = $device['snmp_priv_protocol']; $snmp_context = $device['snmp_context']; $snmp_engine_id = $device['snmp_engine_id']; $max_oids = isset($device['max_oids']) ? $device['max_oids']:10; $device_threads = isset($device['device_threads']) ? $device['device_threads']:1; $external_id = isset($device['external_id']) ? $device['external_id']:''; $location = isset($device['location']) ? $device['location']:''; $bulk_walk_size = isset($device['bulk_walk_size']) ? $device['bulk_walk_size']:-1; automation_debug(' - Adding Device'); $host_id = api_device_save('0', $template_id, $description, $ip, $snmp_community, $snmp_ver, $snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disable, $availability_method, $ping_method, $ping_port, $ping_timeout, $ping_retries, $notes, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_engine_id, $max_oids, $device_threads, $poller_id, $site_id, $external_id, $location, $bulk_walk_size); if ($host_id) { automation_debug(" - Success\n"); /* Use the thold plugin if it exists */ if (api_plugin_is_enabled('thold')) { automation_debug(" Creating Thresholds\n"); if (file_exists($config['base_path'] . '/plugins/thold/thold-functions.php')) { include_once($config['base_path'] . '/plugins/thold/thold-functions.php'); autocreate($host_id); } else if (file_exists($config['base_path'] . '/plugins/thold/thold_functions.php')) { include_once($config['base_path'] . '/plugins/thold/thold_functions.php'); autocreate($host_id); } } db_execute_prepared('DELETE FROM automation_devices WHERE ip = ? LIMIT 1', array($ip)); } else { automation_debug(" - Failed\n"); } return $host_id; } function automation_add_tree($host_id, $tree) { automation_debug(" Adding to tree\n"); if ($tree > 1000000) { $tree_id = $tree - 1000000; $parent = 0; } else { $tree_item = db_fetch_row_prepared('SELECT * FROM graph_tree_items WHERE id = ?', array($tree)); if (!isset($tree_item['graph_tree_id'])) return; $tree_id = $tree_item['graph_tree_id']; $parent = $tree; } $nodeId = api_tree_item_save(0, $tree_id, 3, $parent, '', 0, $host_id, 0, 1, 1, false); } function automation_find_os($sysDescr, $sysObject, $sysName) { $sql_where = ''; $qsysObject = trim(db_qstr($sysObject), "'"); $qsysDescr = trim(db_qstr($sysDescr), "'"); $qsysName = trim(db_qstr($sysName), "'"); $sql_where .= trim($sysDescr) != '' ? 'WHERE (sysDescr REGEXP "(' . preg_quote($qsysDescr) . ')" OR ' . db_qstr($sysDescr) . ' LIKE CONCAT("%", sysDescr, "%"))':''; $sql_where .= trim($sysObject) != '' ? ($sql_where != '' ? ' AND':'WHERE') . ' (sysOID REGEXP "(' . preg_quote($qsysObject) . ')" OR ' . db_qstr($sysObject) . ' LIKE CONCAT("%", sysOid, "%"))':''; $sql_where .= trim($sysName) != '' ? ($sql_where != '' ? ' AND':'WHERE') . ' (sysName REGEXP "(' . preg_quote($qsysName) . ')" OR ' . db_qstr($sysName) . ' LIKE CONCAT("%", sysName, "%"))':''; $result = db_fetch_row("SELECT at.*,ht.name FROM automation_templates AS at INNER JOIN host_template AS ht ON ht.id=at.host_template $sql_where ORDER BY sequence LIMIT 1"); if (cacti_sizeof($result)) { return $result; } else { return false; } } function automation_debug($text) { global $debug, $config; static $message = ''; if (strstr($text, "\n") !== false) { $logLevel = POLLER_VERBOSITY_MEDIUM; if ($debug) { $logLevel = POLLER_VERBOSITY_NONE; } $full_message = trim($message . $text); $messages = explode("\n",$full_message); foreach ($messages as $line) { $line = trim($line); if (strlen($line) > 0) { cacti_log(automation_get_pid() . ' ' . $line, false, 'AUTOM8', $logLevel); } } $message = ''; } else { if (!$config['is_web']) { print $text; } $message .= $text; } } function automation_masktocidr($mask) { $cidr = false; $long = ip2long($mask); if ($long !== false) { $base = ip2long('255.255.255.255'); $cidr = 32 - log(($long ^ $base) + 1, 2); } return $cidr; } function automation_get_valid_ip($range) { $long = ip2long($range); return $long === false ? false : long2ip($long); } function automation_get_valid_subnet_cidr($range) { $long = ip2long($range); if ($long !== false) { $bin = decbin($long); if (strlen($bin) == 32) { $zero = false; $cidr = 0; foreach (str_split($bin) as $char) { if ($char === '0') { $zero = true; } else if ($zero) { $long = false; break; } else { $cidr++; } } } else { $long = false; } } return $long === false ? false : array('cidr' => $cidr, 'subnet' => long2ip($long)); } function automation_get_valid_mask($range) { $cidr = false; if (is_numeric($range)) { if ($range > 0 && $range < 33) { $cidr = $range; $mask = array( 'cidr' => $cidr, 'subnet' => long2ip((pow(2, $range)-1) << (32-$range)), ); } else { $mask = false; } } else { $mask = automation_get_valid_subnet_cidr($range); } if ($mask !== false) { $mask['count'] = bindec(str_repeat('0',$mask['cidr']) . str_repeat('1',32-$mask['cidr'])); if ($mask['count'] == 0) { $mask['count'] = 1; } } return $mask; } function automation_get_network_info($range) { $network = false; $broadcast = false; $mask = array(); $detail = array(); $range = trim($range); if (strpos($range, '/') !== false) { // 10.1.0.0/24 or 10.1.0.0/255.255.255.0 $range_parts = explode('/', $range); if (!filter_var($range_parts[0], FILTER_VALIDATE_IP)) { return false; } $mask = automation_get_valid_mask($range_parts[1]); if (cacti_sizeof($mask)) { $network = automation_get_valid_ip($range_parts[0]); if ($mask['cidr'] != 0) { $dec = ip2long($network) & ip2long($mask['subnet']); $count = $mask['cidr'] == 32 ? 0 : $mask['count']; $network = long2ip($dec); $broadcast = long2ip($dec + $count); } } } elseif (strpos($range, '*') !== false && strpos($range, '-') === false) { $test = str_replace('*', 0, $range); if (!filter_var($test, FILTER_VALIDATE_IP)) { return false; } $range_parts = explode('.', $range); $network = ''; $broadcast = ''; $part_count = 0; foreach ($range_parts as $part) { if ($part != '*') { $part_count++; if (is_numeric($part)) { if ($part >= 0 && $part <= 255) { $network .= $part . '.'; $broadcast .= '255.'; } else { $network = false; break; } } else { $network = false; break; } } else { break; } } if ($part_count == 0 || $part_count > 3) { $network = false; $broadcast = false; } else { while ($part_count < 4) { $part_count += 1; $broadcast .= '0.'; $network .= '0.'; } return automation_get_network_info(rtrim($network,'.').'/'.rtrim($broadcast,'.')); } } elseif (strpos($range, '-') !== false) { raise_message('automation_iprange', __('ERROR: IP ranges in the form of range1-range2 are no longer supported.'), MESSAGE_LEVEL_ERROR); return false; } else { $network = automation_get_valid_ip($range); $broadcast = automation_get_valid_ip($range); } if ($network !== false && $broadcast !== false) { if (ip2long($network) <= ip2long($broadcast)) { $detail['network'] = $network; $detail['broadcast'] = $broadcast; $detail['cidr'] = isset($mask['cidr']) ? $mask['cidr'] : false; if ($network == $broadcast) { $detail['type'] = 'single'; $detail['count'] = 1; $detail['cidr'] = 32; $detail['start'] = $network; $detail['end'] = $network; } else { $detail['type'] = isset($mask['cidr']) ? 'subnet' : 'range'; $detail['count'] = ip2long($broadcast) - ip2long($network) - 1; $detail['start'] = long2ip(ip2long($network) + 1); $detail['end'] = long2ip(ip2long($broadcast) - 1); } } } else { return false; } return $detail; } function automation_calculate_start($range) { $detail = automation_get_network_info($range); if ($detail) { return $detail['start']; } automation_debug(' Could not calculate starting IP!'); return false; } function automation_calculate_total_ips($range) { $detail = automation_get_network_info($range); if ($detail) { return $detail['count']; } automation_debug(' Could not calculate total IPs!'); return false; } function automation_get_next_host($start, $total, $count, $range) { if ($count == $total || $total < 1) { return false; } if (preg_match('/^([0-9]{1,3}\.[0-9]{1,3}\.)\*(\.[0-9]{1,3})$/', $range, $matches)) { // 10.1.*.1 return $matches[1] . ++$count . $matches[2]; } else { // other cases $ip = explode('.', $start); $y = 16777216; for ($x = 0; $x < 4; $x++) { $ip[$x] += intval($count/$y); $count -= ((intval($count/$y))*256); $y = $y / 256; if ($ip[$x] == 256 && $x > 0) { $ip[$x] = 0; $ip[$x-1] += 1; } } return implode('.', $ip); } } function automation_primeIPAddressTable($network_id) { $subNets = db_fetch_cell_prepared('SELECT subnet_range FROM automation_networks WHERE id = ?', array($network_id)); $subNets = explode(',', trim($subNets)); $total = 0; if (cacti_sizeof($subNets)) { foreach($subNets as $position => $subNet) { $count = 1; $sql = array(); $subNetTotal = automation_calculate_total_ips($subNet); $total += $subNetTotal; $start = automation_calculate_start($subNet); if ($start != '') { $sql[] = "('$start', '', $network_id, '0', '0', '0')"; } while ($count < $subNetTotal) { $ip = automation_get_next_host($start, $subNetTotal, $count, $subNet); $count++; if ($ip != '') { $sql[] = "('$ip', '', $network_id, '0', '0', '0')"; } if ($count % 1000 == 0) { db_execute("INSERT INTO automation_ips (ip_address, hostname, network_id, pid, status, thread) VALUES " . implode(',', $sql)); $sql = array(); } } if (cacti_sizeof($sql)) { db_execute("INSERT INTO automation_ips (ip_address, hostname, network_id, pid, status, thread) VALUES " . implode(',', $sql)); } } } automation_debug("A Total of $total IP Addresses Primed\n"); } function automation_valid_snmp_device(&$device) { global $snmp_logging; /* initialize variable */ $host_up = false; $snmp_logging = false; $device['snmp_status'] = HOST_DOWN; $device['ping_status'] = 0; /* force php to return numeric oid's */ cacti_oid_numeric_format(); $snmp_items = db_fetch_assoc_prepared('SELECT * FROM automation_snmp_items WHERE snmp_id = ? ORDER BY sequence ASC', array($device['snmp_id'])); if (cacti_sizeof($snmp_items)) { automation_debug(', SNMP: '); foreach($snmp_items as $item) { // general options $device['snmp_id'] = $item['snmp_id']; $device['snmp_version'] = $item['snmp_version']; $device['snmp_port'] = $item['snmp_port']; $device['snmp_timeout'] = $item['snmp_timeout']; $device['snmp_retries'] = $item['snmp_retries']; // snmp v1/v2 options $device['snmp_community'] = $item['snmp_community']; // snmp v3 options $device['snmp_username'] = $item['snmp_username']; $device['snmp_password'] = $item['snmp_password']; $device['snmp_auth_protocol'] = $item['snmp_auth_protocol']; $device['snmp_priv_passphrase'] = $item['snmp_priv_passphrase']; $device['snmp_priv_protocol'] = $item['snmp_priv_protocol']; $device['snmp_context'] = $item['snmp_context']; $device['snmp_engine_id'] = $item['snmp_engine_id']; $device['max_oids'] = $item['max_oids']; $device['bulk_walk_size'] = $item['bulk_walk_size']; $session = cacti_snmp_session($device['ip_address'], $device['snmp_community'], $device['snmp_version'], $device['snmp_username'], $device['snmp_password'], $device['snmp_auth_protocol'], $device['snmp_priv_passphrase'], $device['snmp_priv_protocol'], $device['snmp_context'], $device['snmp_engine_id'], $device['snmp_port'], $device['snmp_timeout'], $device['snmp_retries'], $device['max_oids']); if ($session !== false) { /* Community string is not used for v3 */ $snmp_sysObjectID = cacti_snmp_session_get($session, '.1.3.6.1.2.1.1.2.0'); if ($snmp_sysObjectID != 'U') { $snmp_sysObjectID = str_replace('enterprises', '.1.3.6.1.4.1', $snmp_sysObjectID); $snmp_sysObjectID = str_replace('OID: ', '', $snmp_sysObjectID); $snmp_sysObjectID = str_replace('.iso', '.1', $snmp_sysObjectID); if ((strlen($snmp_sysObjectID)) && (!substr_count($snmp_sysObjectID, 'No Such Object')) && (!substr_count($snmp_sysObjectID, 'Error In'))) { $snmp_sysObjectID = trim(str_replace('"', '', $snmp_sysObjectID)); $device['snmp_status'] = HOST_UP; $host_up = true; break; } } } if ($host_up == true) { break; } } if ($host_up) { $device['snmp_sysObjectID'] = $snmp_sysObjectID; /* get system name */ $snmp_sysName = cacti_snmp_session_get($session, '.1.3.6.1.2.1.1.5.0'); if ($snmp_sysName != '') { $snmp_sysName = trim(strtr($snmp_sysName,'"',' ')); $device['snmp_sysName'] = $snmp_sysName; automation_debug($snmp_sysName); } else { automation_debug('Unknown System'); } /* get system location */ $snmp_sysLocation = cacti_snmp_session_get($session, '.1.3.6.1.2.1.1.6.0'); if ($snmp_sysLocation != '') { $snmp_sysLocation = trim(strtr($snmp_sysLocation,'"',' ')); $device['snmp_sysLocation'] = $snmp_sysLocation; } /* get system contact */ $snmp_sysContact = cacti_snmp_session_get($session, '.1.3.6.1.2.1.1.4.0'); if ($snmp_sysContact != '') { $snmp_sysContact = trim(strtr($snmp_sysContact,'"',' ')); $device['snmp_sysContact'] = $snmp_sysContact; } /* get system description */ $snmp_sysDescr = cacti_snmp_session_get($session, '.1.3.6.1.2.1.1.1.0'); if ($snmp_sysDescr != '') { $snmp_sysDescr = trim(strtr($snmp_sysDescr,'"',' ')); $device['snmp_sysDescr'] = $snmp_sysDescr; } /* get system uptime */ $snmp_sysUptime = cacti_snmp_session_get($session, '.1.3.6.1.6.3.10.2.1.3.0'); if (!empty($snmp_sysUptime)) { $snmp_sysUptime *= 100; } else { $snmp_sysUptime = cacti_snmp_session_get($session, '.1.3.6.1.2.1.1.3.0'); } if ($snmp_sysUptime != '') { $snmp_sysUptime = trim(strtr($snmp_sysUptime,'"',' ')); $device['snmp_sysUptime'] = $snmp_sysUptime; } $session->close(); } else { automation_debug('No response'); } } return $host_up; } /* gethostbyaddr_wtimeout - This function provides a good method of performing a rapid lookup of a DNS entry for a host so long as you don't have to look far. */ function automation_get_dns_from_ip($ip, $dns, $timeout = 1000) { /* random transaction number (for routers etc to get the reply back) */ $data = rand(10, 99); /* trim it to 2 bytes */ $data = substr($data, 0, 2); /* create request header */ $data .= "\1\0\0\1\0\0\0\0\0\0"; /* split IP into octets */ $octets = explode('.', $ip); /* perform a quick error check */ if (cacti_count($octets) != 4) return 'ERROR'; /* needs a byte to indicate the length of each segment of the request */ for ($x=3; $x>=0; $x--) { switch (strlen($octets[$x])) { case 1: // 1 byte long segment $data .= "\1"; break; case 2: // 2 byte long segment $data .= "\2"; break; case 3: // 3 byte long segment $data .= "\3"; break; default: // segment is too big, invalid IP return 'ERROR'; } /* and the segment itself */ $data .= $octets[$x]; } /* and the final bit of the request */ $data .= "\7in-addr\4arpa\0\0\x0C\0\1"; /* create UDP socket */ $handle = @fsockopen("udp://$dns", 53); @stream_set_timeout($handle, floor($timeout/1000), ($timeout*1000)%1000000); @stream_set_blocking($handle, 1); /* send our request (and store request size so we can cheat later) */ $requestsize = @fwrite($handle, $data); /* get the response */ $response = @fread($handle, 1000); /* check to see if it timed out */ $info = @stream_get_meta_data($handle); /* close the socket */ @fclose($handle); if (isset($info['timed_out'])) { return 'timed_out'; } /* more error handling */ if ($response == '' || $requestsize == false || strlen($response) <= $requestsize) { return $ip; } /* parse the response and find the response type */ $type = @unpack('s', substr($response, $requestsize+2)); if (isset($type[1]) && $type[1] == 0x0C00) { /* set up our variables */ $host = ''; $len = 0; /* set our pointer at the beginning of the hostname uses the request size from earlier rather than work it out. */ $position = $requestsize + 12; /* reconstruct the hostname */ do { /* get segment size */ $len = unpack('c', substr($response, $position)); /* null terminated string, so length 0 = finished */ if ($len[1] == 0) { /* return the hostname, without the trailing '.' */ return strtoupper(substr($host, 0, strlen($host) -1)); } /* add the next segment to our host */ $host .= substr($response, $position+1, $len[1]) . '.'; /* move pointer on to the next segment */ $position += $len[1] + 1; } while ($len != 0); /* error - return the hostname we constructed (without the . on the end) */ return strtoupper($ip); } /* error - return the hostname */ return strtoupper($ip); } function api_automation_is_time_to_start($network_id) { $net = db_fetch_row_prepared('SELECT * FROM automation_networks WHERE id = ?', array($network_id)); $now = time(); switch($net['sched_type']) { case '1': return false; break; case '2': $recur = $net['recur_every'] * 86400; // days $start = strtotime($net['start_at']); $next = strtotime($net['next_start']); if ($net['next_start'] == '0000-00-00 00:00:00') { $target = $start; } else { $target = $next; } if ($now > $target) { while($now > $target) { $target += $recur; } db_execute_prepared('UPDATE automation_networks SET next_start = ? WHERE id = ?', array(date('Y-m-d H:i', $target), $network_id)); return true; break; } return false; break; case '3': $recur = $net['recur_every'] * 86400 * 7; // weeks $start = strtotime($net['start_at']); $next = strtotime($net['next_start']); $days = explode(',', $net['day_of_week']); $day = 86400; $week = 86400 * 7; if ($net['next_start'] == '0000-00-00 00:00:00') { $target = $start; } else { $target = $next; } if ($now > $target) { while(true) { $target += $day; $cur_day = date('w', $target) + 1; $key = array_search($cur_day, $days, false); if ($key !== false && $key >= 0) { if ($key == 0) { $target += $recur - $week; } break; } } db_execute_prepared('UPDATE automation_networks SET next_start = ? WHERE id = ?', array(date('Y-m-d H:i', $target), $network_id)); return true; } return false; break; case '4': case '5': $next = calculateNextStart($net, $now); db_execute_prepared('UPDATE automation_networks SET next_start = ? WHERE id = ?', array(date('Y-m-d H:i', $next), $network_id)); if ($net['next_start'] == '0000-00-00 00:00:00') { if ($now > strtotime($net['start_at'])) { return true; } else { return false; } } elseif ($now > strtotime($net['next_start'])) { return true; } return false; break; } } function calculateNextStart($net) { $now = time(); $dates = array(); switch($net['sched_type']) { case '4': $months = explode(',', $net['month']); $days = explode(',', $net['day_of_month']); foreach($months as $month) { foreach($days as $day) { switch($month) { case '1': $smonth = 'January'; break; case '2': $smonth = 'February'; break; case '3': $smonth = 'March'; break; case '4': $smonth = 'April'; break; case '5': $smonth = 'May'; break; case '6': $smonth = 'June'; break; case '7': $smonth = 'July'; break; case '8': $smonth = 'August'; break; case '9': $smonth = 'September'; break; case '10': $smonth = 'October'; break; case '11': $smonth = 'November'; break; case '12': $smonth = 'December'; break; } if ($day == '32') { $dates[] = strtotime('last day of ' . $smonth);; } else { $dates[] = strtotime("$smonth $day"); } } } break; case '5': $months = explode(',', $net['month']); $weeks = explode(',', $net['monthly_week']); $days = explode(',', $net['monthly_day']); $now = time(); $dates = array(); foreach($months as $month) { foreach($weeks as $week) { foreach($days as $day) { switch($month) { case '1': $smonth = 'January'; break; case '2': $smonth = 'February'; break; case '3': $smonth = 'March'; break; case '4': $smonth = 'April'; break; case '5': $smonth = 'May'; break; case '6': $smonth = 'June'; break; case '7': $smonth = 'July'; break; case '8': $smonth = 'August'; break; case '9': $smonth = 'September'; break; case '10': $smonth = 'October'; break; case '11': $smonth = 'November'; break; case '12': $smonth = 'December'; break; } switch($week) { case '1': $sweek = 'first'; break; case '2': $sweek = 'second'; break; case '3': $sweek = 'third'; break; case '4': $sweek = 'forth'; break; case '32': $sweek = 'last'; break; } switch($day) { case '1': $sday = 'Sunday'; break; case '2': $sday = 'Monday'; break; case '3': $sday = 'Tuesday'; break; case '4': $sday = 'Wednesday'; break; case '5': $sday = 'Thursday'; break; case '6': $sday = 'Friday'; break; case '7': $sday = 'Saturday'; break; } $dates[] = strtotime("$sweek $sday of $smonth", strtotime($net['start_at'])); } } } break; } asort($dates); $newdates = array(); foreach($dates as $date) { $ndate = date('Y-m-d', $date) . ' ' . date('H:i:s', strtotime($net['start_at'])); $ntime = strtotime($ndate); automation_debug('Start At: ' . $net['start_at'] . ', Possible Next Start: ' . $ndate . ' with Timestamp: ' . $ntime); if ($ntime > $now) { return $ntime; } } return false; } function ping_netbios_name($ip, $timeout_ms = 1000) { $handle = @fsockopen("udp://$ip", 137); if (is_resource($handle)) { stream_set_timeout($handle, floor($timeout_ms/1000), ($timeout_ms*1000)%1000000); stream_set_blocking($handle, 1); $packet = "\x99\x99\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x20\x43\x4b" . str_repeat("\x41", 30) . "\x00\x00\x21\x00\x01"; /* send our request (and store request size so we can cheat later) */ $requestsize = @fwrite($handle, $packet); /* get the response */ $response = @fread($handle, 2048); /* check to see if it timed out */ $info = @stream_get_meta_data($handle); /* close the socket */ fclose($handle); if ($info['timed_out']) { return false; } if (!isset($response[56])) { return false; } /* parse the response and find the response type */ $names = hexdec(ord($response[56])); if ($names > 0) { $host = ''; for($i=57;$i