#!/usr/bin/env php /cli/ and netavo_bng_subscribers.xml to * /resource/snmp_queries/, then run as the user the poller runs as: * * --install Create (once; safe to repeat) the data query, the data and graph * templates, their mapping and the automation rule. * --add-host --host-id=N Attach the data query to a device and graph its subscribers. * --sync [--host-id=N] Re-index and graph any new subscribers, then bring the * per-subscriber aggregate graphs in line. Run this from cron. * --status [--host-id=N] Rows, graphs and orphans per device. Changes nothing. * --prune [--host-id=N] List graphs whose subscriber the appliance no longer reports * [--apply] (unseen for the table's retention period). --apply deletes * them and their RRD-backed data sources. * * One graph per subscriber, whichever appliance they are on: the table's indexes are per * appliance, so a line that redials onto another appliance continues on that one's graph. * For every subscriber name --sync keeps an aggregate graph, "Subscriber - ", that sums * the subscriber's graphs from every device. Only one of them moves at a time, so the sum is * the subscriber's traffic with no gap at a move. Aggregates hold no data of their own; they * are on the graph tree under "BNG Subscribers". * * Why it does not use the stock interface templates as they are: * - ifHighSpeed is 0 for an unshaped plan, and the stock data template takes its maximum * from the interface speed. A maximum of 0 discards every sample. * - "In" on the table is traffic from the subscriber. A subscriber graph should say * Download and Upload, with download as the filled area. */ foreach (array('/../include/cli_check.php', '/../site/include/cli_check.php') as $inc) { if (file_exists(__DIR__ . $inc)) { require(__DIR__ . $inc); break; } } if (!isset($config)) { fwrite(STDERR, "FATAL: not in a Cacti cli/ directory (include/cli_check.php not found)\n"); exit(1); } require_once($config['base_path'] . '/lib/api_aggregate.php'); require_once($config['base_path'] . '/lib/api_automation_tools.php'); require_once($config['base_path'] . '/lib/api_automation.php'); require_once($config['base_path'] . '/lib/api_data_source.php'); require_once($config['base_path'] . '/lib/api_graph.php'); require_once($config['base_path'] . '/lib/api_device.php'); require_once($config['base_path'] . '/lib/api_tree.php'); require_once($config['base_path'] . '/lib/data_query.php'); require_once($config['base_path'] . '/lib/poller.php'); require_once($config['base_path'] . '/lib/snmp.php'); require_once($config['base_path'] . '/lib/sort.php'); require_once($config['base_path'] . '/lib/template.php'); require_once($config['base_path'] . '/lib/utility.php'); define('DQ_NAME', 'NetavoBNG - Subscribers'); define('DQ_XML', 'netavo_bng_subscribers.xml'); define('DT_NAME', 'NetavoBNG - Subscriber Traffic'); define('GT_NAME', 'NetavoBNG - Subscriber Traffic (bits/sec)'); define('SQG_NAME', 'Download/Upload Bits (64-bit)'); define('RULE_NAME', 'NetavoBNG - Subscriber Graphs'); define('TITLE', '|host_description| - Subscriber - |query_ifName|'); define('SRC_DT_NAME', 'Interface - Traffic'); define('SRC_GT_NAME', 'Interface - Traffic (bits/sec)'); /* bytes/sec. 100 Gbit/s: above any one subscriber, below what a wrapped counter produces. */ define('RRD_MAXIMUM', '12500000000'); define('HEX_DOWNLOAD', '00CF00'); define('HEX_UPLOAD', '002A97'); define('GRAPH_ITEM_LINE1', 4); define('GRAPH_ITEM_AREA', 7); define('GRAPH_ITEM_GPRINT', 9); define('AGG_TITLE', 'Subscriber - '); define('TREE_BRANCH', 'BNG Subscribers'); $mode = ''; $host_id = 0; $apply = false; foreach (array_slice($_SERVER['argv'], 1) as $arg) { if (preg_match('/^--(install|add-host|sync|status|prune)$/', $arg, $m)) { $mode = $m[1]; } elseif (preg_match('/^--host-id=(\d+)$/', $arg, $m)) { $host_id = (int)$m[1]; } elseif ($arg == '--apply') { $apply = true; } else { fwrite(STDERR, "unknown argument: $arg\n"); exit(1); } } if ($mode == '') { fwrite(STDERR, "usage: bng_cacti_subscribers.php --install | --add-host --host-id=N | --sync [--host-id=N]" . " | --status [--host-id=N] | --prune [--host-id=N] [--apply]\n"); exit(1); } function fatal($msg) { fwrite(STDERR, "FATAL: $msg\n"); exit(1); } function query_id() { return (int)db_fetch_cell_prepared('SELECT id FROM snmp_query WHERE name = ?', array(DQ_NAME)); } function color_id($hex) { $id = db_fetch_cell_prepared('SELECT id FROM colors WHERE hex = ?', array($hex)); if (!$id) { db_execute_prepared('INSERT INTO colors (name, hex) VALUES (?, ?)', array($hex, $hex)); $id = db_fetch_insert_id(); } return (int)$id; } /* The template data source items of a data template, keyed by data source name. */ function template_rrds($data_template_id) { return array_rekey(db_fetch_assoc_prepared('SELECT id, data_source_name FROM data_template_rrd WHERE data_template_id = ? AND local_data_id = 0', array($data_template_id)), 'data_source_name', 'id'); } function ensure_data_query() { global $config; if (!file_exists($config['base_path'] . '/resource/snmp_queries/' . DQ_XML)) { fatal('copy ' . DQ_XML . ' to ' . $config['base_path'] . '/resource/snmp_queries/ first'); } $id = query_id(); if ($id) { echo "data query exists: " . DQ_NAME . " (id $id)\n"; return $id; } $input = db_fetch_cell("SELECT id FROM data_input WHERE hash = 'bf566c869ac6443b0c75d1c32b5a350e'"); if (!$input) { $input = db_fetch_cell("SELECT id FROM data_input WHERE name = 'Get SNMP Data (Indexed)'"); } if (!$input) fatal("data input method 'Get SNMP Data (Indexed)' not found"); $id = sql_save(array( 'id' => 0, 'hash' => get_hash_data_query(0), 'xml_path' => '/resource/snmp_queries/' . DQ_XML, 'name' => DQ_NAME, 'description' => 'Per-subscriber traffic from NETAVO-BNG-MIB bngSubscriberTable', 'graph_template_id' => 0, 'data_input_id' => $input), 'snmp_query'); if (!$id) fatal('could not create the data query'); echo "created data query: " . DQ_NAME . " (id $id)\n"; return $id; } function ensure_data_template() { $id = db_fetch_cell_prepared('SELECT id FROM data_template WHERE name = ?', array(DT_NAME)); if ($id) { echo "data template exists: " . DT_NAME . " (id $id)\n"; return (int)$id; } $src = db_fetch_cell_prepared('SELECT id FROM data_template WHERE name = ?', array(SRC_DT_NAME)); if (!$src) fatal("stock data template '" . SRC_DT_NAME . "' not found"); api_duplicate_data_source(0, $src, DT_NAME); $id = (int)db_fetch_cell_prepared('SELECT id FROM data_template WHERE name = ?', array(DT_NAME)); if (!$id) fatal('data template duplication failed'); /* a fixed maximum, not one taken from the (zero) interface speed */ db_execute_prepared("UPDATE data_template_rrd SET rrd_maximum = ?, t_rrd_maximum = '', rrd_minimum = '0' WHERE data_template_id = ? AND local_data_id = 0", array(RRD_MAXIMUM, $id)); db_execute_prepared("UPDATE data_template_data SET name = ?, t_name = 'on' WHERE data_template_id = ? AND local_data_id = 0", array(TITLE, $id)); echo "created data template: " . DT_NAME . " (id $id)\n"; return $id; } function ensure_graph_template($data_template_id) { $id = db_fetch_cell_prepared('SELECT id FROM graph_templates WHERE name = ?', array(GT_NAME)); if ($id) { echo "graph template exists: " . GT_NAME . " (id $id)\n"; return (int)$id; } $src = db_fetch_cell_prepared('SELECT id FROM graph_templates WHERE name = ?', array(SRC_GT_NAME)); if (!$src) fatal("stock graph template '" . SRC_GT_NAME . "' not found"); $src_dt = db_fetch_cell_prepared('SELECT id FROM data_template WHERE name = ?', array(SRC_DT_NAME)); $old = template_rrds($src_dt); $new = template_rrds($data_template_id); foreach (array('traffic_in', 'traffic_out') as $ds) { if (!isset($old[$ds]) || !isset($new[$ds])) fatal("data source '$ds' missing from a traffic data template"); } api_duplicate_graph(0, $src, GT_NAME, false); $id = (int)db_fetch_cell_prepared('SELECT id FROM graph_templates WHERE name = ?', array(GT_NAME)); if (!$id) fatal('graph template duplication failed'); /* Keep, per direction: the filled/line series with its legend, and its three GPRINTs. Local additions to the stock template (extra unlabelled lines, negative-stack items) are dropped. */ $items = db_fetch_assoc_prepared('SELECT id, task_item_id, graph_type_id, text_format FROM graph_templates_item WHERE graph_template_id = ? AND local_graph_id = 0 ORDER BY sequence, id', array($id)); $kept = array($old['traffic_in'] => array(), $old['traffic_out'] => array()); foreach ($items as $i) { $series = $i['graph_type_id'] != 9 && $i['text_format'] != ''; $gprint = $i['graph_type_id'] == 9; if (isset($kept[$i['task_item_id']]) && ($series || $gprint) && cacti_sizeof($kept[$i['task_item_id']]) < 4) { $kept[$i['task_item_id']][] = $i; } else { db_execute_prepared('DELETE FROM graph_template_input_defs WHERE graph_template_item_id = ?', array($i['id'])); db_execute_prepared('DELETE FROM graph_templates_item WHERE id = ?', array($i['id'])); } } /* The stock template leads with its inbound series. Here the leading, filled series is the subscriber's download, which the table reports as "out". */ $seq = 1; $sides = array( array($old['traffic_in'], $new['traffic_out'], 'Download', GRAPH_ITEM_AREA, color_id(HEX_DOWNLOAD)), array($old['traffic_out'], $new['traffic_in'], 'Upload', GRAPH_ITEM_LINE1, color_id(HEX_UPLOAD))); foreach ($sides as $s) { list($old_task, $new_task, $label, $type, $color) = $s; if (cacti_sizeof($kept[$old_task]) != 4) fatal("stock template '" . SRC_GT_NAME . "' is not shaped as expected"); foreach ($kept[$old_task] as $i) { if ($i['graph_type_id'] == 9) { db_execute_prepared('UPDATE graph_templates_item SET task_item_id = ?, sequence = ? WHERE id = ?', array($new_task, $seq++, $i['id'])); } else { db_execute_prepared('UPDATE graph_templates_item SET task_item_id = ?, sequence = ?, text_format = ?, graph_type_id = ?, color_id = ? WHERE id = ?', array($new_task, $seq++, $label, $type, $color, $i['id'])); } } db_execute_prepared('UPDATE graph_template_input SET name = ? WHERE graph_template_id = ? AND id IN (SELECT graph_template_input_id FROM graph_template_input_defs WHERE graph_template_item_id = ?)', array($label . ' Data Source', $id, $kept[$old_task][0]['id'])); } db_execute_prepared("UPDATE graph_templates_graph SET title = ?, t_title = 'on', vertical_label = 'bits per second' WHERE graph_template_id = ? AND local_graph_id = 0", array(TITLE, $id)); echo "created graph template: " . GT_NAME . " (id $id)\n"; return $id; } function ensure_mapping($query_id, $graph_template_id, $data_template_id) { $id = (int)db_fetch_cell_prepared('SELECT id FROM snmp_query_graph WHERE snmp_query_id = ? AND graph_template_id = ?', array($query_id, $graph_template_id)); if ($id) { echo "mapping exists: " . SQG_NAME . " (id $id)\n"; return $id; } $id = sql_save(array( 'id' => 0, 'hash' => get_hash_data_query(0, 'data_query_graph'), 'snmp_query_id' => $query_id, 'name' => SQG_NAME, 'graph_template_id' => $graph_template_id), 'snmp_query_graph'); if (!$id) fatal('could not create the data query graph mapping'); $rrds = template_rrds($data_template_id); foreach (array('traffic_in' => 'ifHCInOctets', 'traffic_out' => 'ifHCOutOctets') as $ds => $field) { db_execute_prepared('REPLACE INTO snmp_query_graph_rrd (snmp_query_graph_id, data_template_id, data_template_rrd_id, snmp_field_name) VALUES (?, ?, ?, ?)', array($id, $data_template_id, $rrds[$ds], $field)); } db_execute_prepared("INSERT INTO snmp_query_graph_sv (hash, snmp_query_graph_id, sequence, field_name, text) VALUES (?, ?, 1, 'title', ?)", array(get_hash_data_query(0, 'data_query_sv_graph'), $id, TITLE)); db_execute_prepared("INSERT INTO snmp_query_graph_rrd_sv (hash, snmp_query_graph_id, data_template_id, sequence, field_name, text) VALUES (?, ?, ?, 1, 'name', ?)", array(get_hash_data_query(0, 'data_query_sv_data_source'), $id, $data_template_id, TITLE)); echo "created mapping: " . SQG_NAME . " (id $id)\n"; return $id; } function ensure_rule($query_id, $sqg_id) { $id = (int)db_fetch_cell_prepared('SELECT id FROM automation_graph_rules WHERE name = ?', array(RULE_NAME)); if ($id) { echo "automation rule exists: " . RULE_NAME . " (id $id)\n"; return $id; } $id = sql_save(array('id' => 0, 'name' => RULE_NAME, 'snmp_query_id' => $query_id, 'graph_type_id' => $sqg_id, 'enabled' => 'on'), 'automation_graph_rules'); if (!$id) fatal('could not create the automation rule'); /* Every device the data query is attached to; every row that has a name. Attaching the query (--add-host) is what opts a device in. */ db_execute_prepared("INSERT INTO automation_match_rule_items (rule_id, rule_type, sequence, operation, field, operator, pattern) VALUES (?, ?, 1, ?, 'h.description', ?, '')", array($id, AUTOMATION_RULE_TYPE_GRAPH_MATCH, AUTOMATION_OPER_NULL, AUTOMATION_OP_NOT_EMPTY)); db_execute_prepared("INSERT INTO automation_graph_rule_items (rule_id, sequence, operation, field, operator, pattern) VALUES (?, 1, ?, 'ifName', ?, '')", array($id, AUTOMATION_OPER_NULL, AUTOMATION_OP_NOT_EMPTY)); echo "created automation rule: " . RULE_NAME . " (id $id)\n"; return $id; } function hosts_with_query($query_id, $host_id) { $sql = "SELECT h.id, h.description FROM host h JOIN host_snmp_query q ON q.host_id = h.id WHERE q.snmp_query_id = ? AND h.deleted = ''"; $args = array($query_id); if ($host_id) { $sql .= ' AND h.id = ?'; $args[] = $host_id; } return db_fetch_assoc_prepared($sql . ' ORDER BY h.id', $args); } function counts($host_id, $query_id) { return array( 'rows' => (int)db_fetch_cell_prepared("SELECT COUNT(*) FROM host_snmp_cache WHERE host_id = ? AND snmp_query_id = ? AND field_name = 'ifName'", array($host_id, $query_id)), 'graphs' => (int)db_fetch_cell_prepared('SELECT COUNT(*) FROM graph_local WHERE host_id = ? AND snmp_query_id = ?', array($host_id, $query_id))); } /* Graphs whose row the appliance has stopped reporting. */ function orphans($host_id, $query_id) { return db_fetch_assoc_prepared("SELECT gl.id, gtg.title_cache FROM graph_local gl JOIN graph_templates_graph gtg ON gtg.local_graph_id = gl.id LEFT JOIN host_snmp_cache c ON c.host_id = gl.host_id AND c.snmp_query_id = gl.snmp_query_id AND c.snmp_index = gl.snmp_index AND c.field_name = 'ifName' WHERE gl.host_id = ? AND gl.snmp_query_id = ? AND c.snmp_index IS NULL ORDER BY gl.id", array($host_id, $query_id)); } function sync_host($host, $query_id) { $before = counts($host['id'], $query_id); run_data_query($host['id'], $query_id); $after = counts($host['id'], $query_id); /* A walk that returns nothing leaves the cache empty; say so, since every graph on the device then looks orphaned. */ $note = $after['rows'] == 0 ? ' ** the table returned no rows - check SNMP access to it **' : ''; printf("%s (host %d): %d subscribers, %d graphs (%+d new)%s\n", $host['description'], $host['id'], $after['rows'], $after['graphs'], $after['graphs'] - $before['graphs'], $note); return $after; } /* The "BNG Subscribers" branch of the first graph tree, created on first use. */ function tree_branch() { $tree = (int)db_fetch_cell('SELECT MIN(id) FROM graph_tree'); if (!$tree) return array(0, 0); $branch = (int)db_fetch_cell_prepared('SELECT id FROM graph_tree_items WHERE graph_tree_id = ? AND parent = 0 AND local_graph_id = 0 AND host_id = 0 AND title = ?', array($tree, TREE_BRANCH)); if (!$branch) { $branch = (int)api_tree_item_save(0, $tree, TREE_ITEM_TYPE_HEADER, 0, TREE_BRANCH, 0, 0, 0, 1, TREE_ORDERING_ALPHABETIC, false); } return array($tree, $branch); } /* One aggregate graph per subscriber name, summing that subscriber's graph from every device. Returns array(created, updated). */ function sync_aggregates($query_id) { $gt = (int)db_fetch_cell_prepared('SELECT id FROM graph_templates WHERE name = ?', array(GT_NAME)); if (!$gt) return array(0, 0); /* A device whose table came back empty has no rows cached. Rebuilding now would drop its graph from every aggregate; wait for a run in which every device answered. */ foreach (hosts_with_query($query_id, 0) as $host) { $c = counts($host['id'], $query_id); if ($c['rows'] == 0 && $c['graphs'] > 0) { echo "aggregates left as they are: {$host['description']} returned no rows\n"; return array(0, 0); } } $want = array(); foreach (db_fetch_assoc_prepared("SELECT c.field_value AS name, gl.id FROM host_snmp_cache c JOIN graph_local gl ON gl.host_id = c.host_id AND gl.snmp_query_id = c.snmp_query_id AND gl.snmp_index = c.snmp_index JOIN host h ON h.id = c.host_id AND h.deleted = '' WHERE c.snmp_query_id = ? AND c.field_name = 'ifName' AND c.field_value <> '' AND gl.graph_template_id = ? ORDER BY c.field_value, gl.host_id, gl.id", array($query_id, $gt)) as $r) { $want[AGG_TITLE . $r['name']][] = (int)$r['id']; } $have = array(); foreach (db_fetch_assoc_prepared('SELECT ag.id, ag.local_graph_id, ag.title_format, (SELECT GROUP_CONCAT(i.local_graph_id ORDER BY i.sequence) FROM aggregate_graphs_items i WHERE i.aggregate_graph_id = ag.id) AS members FROM aggregate_graphs ag WHERE ag.graph_template_id = ?', array($gt)) as $r) { $have[$r['title_format']] = $r; } $template_items = db_fetch_assoc_prepared('SELECT id, sequence FROM graph_templates_item WHERE graph_template_id = ? AND local_graph_id = 0 ORDER BY sequence', array($gt)); $total = array(); foreach ($template_items as $i) { $total[$i['sequence']] = $i['sequence']; } $rrds = template_rrds((int)db_fetch_cell_prepared('SELECT id FROM data_template WHERE name = ?', array(DT_NAME))); list($tree, $branch) = tree_branch(); /* aggregate_create_update records who made the graph */ if (!isset($_SESSION['sess_user_id'])) { $_SESSION['sess_user_id'] = (int)db_fetch_cell('SELECT MIN(id) FROM user_auth'); } $created = 0; $updated = 0; foreach ($want as $title => $members) { $existing = isset($have[$title]) ? $have[$title] : null; if ($existing && $existing['members'] === implode(',', $members)) continue; $local_graph_id = $existing ? (int)$existing['local_graph_id'] : 0; $attribs = array( 'graph_title' => $title, 'aggregate_template_id' => 0, 'template_propogation' => '', 'graph_template_id' => $gt, 'gprint_prefix' => '', 'gprint_format' => '', 'graph_type' => AGGREGATE_GRAPH_TYPE_KEEP, 'total' => AGGREGATE_TOTAL_ONLY, 'total_type' => AGGREGATE_TOTAL_TYPE_SIMILAR, 'total_prefix' => '', 'reorder' => AGGREGATE_ORDER_NONE, 'item_no' => cacti_sizeof($template_items), 'color_templates' => array(), 'skipped_items' => array(), 'total_items' => $total, 'graph_item_types' => array(), 'cdefs' => array()); aggregate_create_update($local_graph_id, $members, $attribs); if (!$local_graph_id) { echo "WARNING: could not create the aggregate '$title'\n"; continue; } $aggregate_id = (int)db_fetch_cell_prepared('SELECT id FROM aggregate_graphs WHERE local_graph_id = ?', array($local_graph_id)); /* a member that left takes its row with it */ if (cacti_sizeof($members)) { db_execute_prepared('DELETE FROM aggregate_graphs_items WHERE aggregate_graph_id = ? AND local_graph_id NOT IN (' . implode(',', $members) . ')', array($aggregate_id)); } /* The totals are the template's own items (the members' are drawn at zero height): give the two series their legend back, at full opacity. */ foreach (array('traffic_out' => 'Download', 'traffic_in' => 'Upload') as $ds => $label) { db_execute_prepared("UPDATE graph_templates_item SET text_format = ?, alpha = 'FF' WHERE local_graph_id = ? AND task_item_id = ? AND graph_type_id <> ?", array($label, $local_graph_id, $rrds[$ds], GRAPH_ITEM_GPRINT)); } /* what the aggregate's edit page shows as its settings */ foreach ($template_items as $i) { db_execute_prepared("REPLACE INTO aggregate_graphs_graph_item (aggregate_graph_id, graph_templates_item_id, sequence, color_template, t_graph_type_id, graph_type_id, t_cdef_id, cdef_id, item_skip, item_total) VALUES (?, ?, ?, 0, '', 0, '', 0, '', 'on')", array($aggregate_id, $i['id'], $i['sequence'])); } if ($existing) { $updated++; } else { $created++; if ($branch) { api_tree_item_save(0, $tree, TREE_ITEM_TYPE_GRAPH, $branch, '', $local_graph_id, 0, 0, 1, TREE_ORDERING_INHERIT, false); } } } return array($created, $updated); } /* Aggregates whose subscriber no device reports any more. */ function orphan_aggregates($query_id) { $gt = (int)db_fetch_cell_prepared('SELECT id FROM graph_templates WHERE name = ?', array(GT_NAME)); return db_fetch_assoc_prepared("SELECT ag.local_graph_id AS id, ag.title_format AS title_cache FROM aggregate_graphs ag WHERE ag.graph_template_id = ? AND ag.title_format LIKE ? AND NOT EXISTS (SELECT 1 FROM host_snmp_cache c JOIN host h ON h.id = c.host_id AND h.deleted = '' WHERE c.snmp_query_id = ? AND c.field_name = 'ifName' AND CONCAT(?, c.field_value) = ag.title_format) ORDER BY ag.title_format", array($gt, AGG_TITLE . '%', $query_id, AGG_TITLE)); } function report_aggregates($query_id) { list($created, $updated) = sync_aggregates($query_id); $n = (int)db_fetch_cell_prepared('SELECT COUNT(*) FROM aggregate_graphs ag JOIN graph_templates gt ON gt.id = ag.graph_template_id WHERE gt.name = ?', array(GT_NAME)); printf("aggregates: %d subscribers (%d new, %d changed members)\n", $n, $created, $updated); } if ($mode == 'install') { $q = ensure_data_query(); $dt = ensure_data_template(); $gt = ensure_graph_template($dt); $sqg = ensure_mapping($q, $gt, $dt); ensure_rule($q, $sqg); if (read_config_option('automation_graphs_enabled') != 'on') { echo "NOTE: graph automation is off (Console > Configuration > Settings > General); --sync will not create graphs until it is on\n"; } exit(0); } $query_id = query_id(); if (!$query_id) fatal('run --install first'); if ($mode == 'add-host') { if (!$host_id) fatal('--add-host needs --host-id=N'); $host = db_fetch_row_prepared("SELECT id, description FROM host WHERE id = ? AND deleted = ''", array($host_id)); if (!cacti_sizeof($host)) fatal("no device with id $host_id"); db_execute_prepared('INSERT INTO host_snmp_query (host_id, snmp_query_id, sort_field, title_format, reindex_method) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE reindex_method = VALUES(reindex_method)', array($host_id, $query_id, 'ifName', '|chosen_order_field|', DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE)); sync_host($host, $query_id); report_aggregates($query_id); exit(0); } $hosts = hosts_with_query($query_id, $host_id); if (!cacti_sizeof($hosts)) { echo "no device has the data query attached" . ($host_id ? " (host $host_id)" : '') . " - use --add-host\n"; exit(0); } $all_answered = true; foreach ($hosts as $host) { if ($mode == 'sync') { sync_host($host, $query_id); continue; } $c = counts($host['id'], $query_id); $gone = orphans($host['id'], $query_id); if ($mode == 'status') { printf("%s (host %d): %d subscribers, %d graphs, %d orphaned\n", $host['description'], $host['id'], $c['rows'], $c['graphs'], cacti_sizeof($gone)); continue; } /* prune */ if ($c['rows'] == 0) { printf("%s (host %d): no rows cached - not pruning (run --sync and check SNMP access first)\n", $host['description'], $host['id']); $all_answered = false; continue; } foreach ($gone as $g) { echo ($apply ? 'removing' : 'would remove') . ": graph {$g['id']} {$g['title_cache']}\n"; } if ($apply && cacti_sizeof($gone)) { $graph_ids = array_column($gone, 'id'); $data_ids = array_column(db_fetch_assoc('SELECT DISTINCT dtr.local_data_id FROM graph_templates_item gti JOIN data_template_rrd dtr ON dtr.id = gti.task_item_id JOIN graph_local gl ON gl.id = gti.local_graph_id AND gl.host_id > 0 WHERE dtr.local_data_id > 0 AND gti.local_graph_id IN (' . implode(',', array_map('intval', $graph_ids)) . ')'), 'local_data_id'); api_graph_remove_multi($graph_ids); if (cacti_sizeof($data_ids)) { api_data_source_remove_multi($data_ids); } } printf("%s (host %d): %d orphaned graph(s) %s\n", $host['description'], $host['id'], cacti_sizeof($gone), $apply ? 'removed' : 'found; --apply to remove them'); } if ($mode == 'sync') { report_aggregates($query_id); } elseif ($mode == 'status') { printf("aggregates: %d, %d of them for a subscriber no device reports\n", (int)db_fetch_cell_prepared('SELECT COUNT(*) FROM aggregate_graphs ag JOIN graph_templates gt ON gt.id = ag.graph_template_id WHERE gt.name = ?', array(GT_NAME)), cacti_sizeof(orphan_aggregates($query_id))); } elseif ($mode == 'prune' && !$host_id && $all_answered) { /* An aggregate goes when no device reports its subscriber. It holds no data. */ $gone = orphan_aggregates($query_id); foreach ($gone as $g) { echo ($apply ? 'removing' : 'would remove') . ": aggregate {$g['id']} {$g['title_cache']}\n"; } if ($apply && cacti_sizeof($gone)) { $ids = array_map('intval', array_column($gone, 'id')); api_graph_remove_multi($ids); db_execute('DELETE FROM aggregate_graphs_items WHERE aggregate_graph_id IN (SELECT id FROM aggregate_graphs WHERE local_graph_id IN (' . implode(',', $ids) . '))'); db_execute('DELETE FROM aggregate_graphs_graph_item WHERE aggregate_graph_id IN (SELECT id FROM aggregate_graphs WHERE local_graph_id IN (' . implode(',', $ids) . '))'); db_execute('DELETE FROM aggregate_graphs WHERE local_graph_id IN (' . implode(',', $ids) . ')'); if ($apply) sync_aggregates($query_id); } printf("%d orphaned aggregate(s) %s\n", cacti_sizeof($gone), $apply ? 'removed' : 'found; --apply to remove them'); }