work around missing id, RT#83146
[freeside.git] / bin / freeside_cacti.php
1 #!/usr/bin/php -q
2 <?php
3 /*
4  +-------------------------------------------------------------------------+
5  | Copyright (C) 2015 Freeside Internet Services                           |
6  |                                                                         |
7  | This program is free software; you can redistribute it and/or           |
8  | modify it under the terms of the GNU General Public License             |
9  | as published by the Free Software Foundation; either version 2          |
10  | of the License, or (at your option) any later version.                  |
11  |                                                                         |
12  | This program is distributed in the hope that it will be useful,         |
13  | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
14  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
15  | GNU General Public License for more details.                            |
16  +-------------------------------------------------------------------------+
17  | Copy this file to the cli directory of your Cacti installation, which   |
18  | should also contain an add_device.php script.  Give this file the same  |
19  | permissions as add_device.php, and configure your Freeside installation |
20  | with the location of that directory and the name of a user who has      |
21  | permission to read these files.  See the FS::part_export::cacti docs    |
22  | for more details.                                                       |
23  +-------------------------------------------------------------------------+
24 */
25
26 /* do NOT run this script through a web browser */
27 if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD'])  || isset($_SERVER['REMOTE_ADDR'])) {
28         die("<br><strong>This script is only meant to run at the command line.</strong>");
29 }
30
31 /* We are not talking to the browser */
32 $no_http_headers = true;
33
34 /* process calling arguments */
35 $action = '';
36 $ip = '';
37 $host_template = '';
38 $include_path = "../site/include/";
39 $delete_graphs = FALSE;
40 $parms = $_SERVER["argv"];
41 array_shift($parms);
42 if (sizeof($parms)) {
43         foreach($parms as $parameter) {
44                 @list($arg, $value) = @explode("=", $parameter);
45                 switch ($arg) {
46         case "--get-graphs":
47                         $action = 'get-graphs';
48             break;
49         case "--drop-device":
50                         $action = 'drop-device';
51             break;
52 /*
53         case "--get-device":
54                         $action = 'get-device';
55             break;
56 */
57         case "--get-graph-templates":
58                         $action = 'get-graph-templates';
59             break;
60                 case "--ip":
61                         $ip = trim($value);
62                         break;
63                 case "--host-template":
64                         $host_template = trim($value);
65                         break;
66                 case "--delete-graphs":
67                         $delete_graphs = TRUE;
68                         break;
69                 case "--include-path":
70                         $include_path = trim($value);
71                         break;
72                 case "--version":
73                 case "-V":
74                 case "-H":
75                 case "--help":
76                         die(default_die());
77                 default:
78                         die("ERROR: Invalid Argument: ($arg)");
79                 }
80         }
81 } else {
82   die(default_die());
83 }
84
85 $include_path = dirname(__FILE__).'/'.$include_path.'/global.php';
86 if (!file_exists($include_path)) {
87   die("File " . $include_path . "/global.php not found (check include_path in freeside export config)");
88 }
89 include($include_path);
90 include_once($config["base_path"]."/lib/api_device.php");
91 include_once($config["base_path"]."/lib/api_automation_tools.php");
92 include_once($config["base_path"]."/lib/api_data_source.php");
93 include_once($config["base_path"]."/lib/api_graph.php");
94 include_once($config["base_path"]."/lib/functions.php");
95
96 /* Now take an action */
97 switch ($action) {
98 case "get-graphs":
99         displayHostGraphs(host_id($ip),TRUE);
100         break;
101 case "drop-device":
102         $host_id = host_id($ip);
103         if ($delete_graphs) {
104                 // code copied & pasted from version 0.8.8a
105         // cacti/site/lib/host.php and cacti/site/graphs.php 
106                 // unfortunately no api function for this yet
107                 $graphs = db_fetch_assoc("select
108                         graph_local.id as local_graph_id
109                         from graph_local
110                         where graph_local.host_id=" . $host_id);
111                 if (sizeof($graphs) > 0) {
112                         foreach ($graphs as $graph) {
113                                 $data_sources = array_rekey(db_fetch_assoc("SELECT data_template_data.local_data_id
114                                         FROM (data_template_rrd, data_template_data, graph_templates_item)
115                                         WHERE graph_templates_item.task_item_id=data_template_rrd.id
116                                         AND data_template_rrd.local_data_id=data_template_data.local_data_id
117                                         AND graph_templates_item.local_graph_id=" . $graph["local_graph_id"] . "
118                                         AND data_template_data.local_data_id > 0"), "local_data_id", "local_data_id");
119                                 if (sizeof($data_sources)) {
120                                         api_data_source_remove_multi($data_sources);
121                                 }
122                                 api_graph_remove($graph["local_graph_id"]);
123                         }
124                 }
125         }
126         api_device_remove($host_id);
127         if (host_id($ip,1)) {
128                 die("Failed to remove hostname $ip");
129         }
130         exit(0);
131 /*
132 case "get-device":
133         echo host_id($ip);
134         exit(0);
135 */
136 case "get-graph-templates":
137         if (!$host_template) {
138                 die("No host template specified");
139         }
140         $graphs = getGraphTemplatesByHostTemplate($host_template);
141         if (sizeof($graphs)) {
142                 foreach (array_keys($graphs) as $gtid) {
143                         echo $gtid . "\n";
144                 }
145                 exit(0);
146         }
147         die("No graph templates associated with this host template");
148 default:
149         die("Specified action not found, contact a developer");
150 }
151
152 function default_die() {
153   return "Cacti interface for freeside.  Do not use for anything else.";
154 }
155
156 function host_id($ip_address, $nodie=0) {
157         if (!$ip_address) {
158                 die("No hostname specified");
159         }
160         $devices = array();
161         $query = "select id from host";
162         $query .= " where hostname='$ip_address'";
163         $devices = db_fetch_assoc($query);
164         if (sizeof($devices) > 1) {
165         // This should never happen, just being thorough
166                 die("Multiple devices found for hostname $ip_address");
167         } else if (!sizeof($devices)) {
168                 if ($nodie) {
169                         return '';
170                 } else {
171                         die("Could not find hostname $ip_address");
172                 }
173         }
174         return $devices[0]['id'];
175 }
176
177 ?>