RT#18834: Cacti integration [real graph import]
[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 /* 
35 Currently, only drop-device and get-graphs is actually being used by Freeside integration,
36 but keeping commented out code for potential future development.
37 */
38
39 include(dirname(__FILE__)."/../site/include/global.php");
40 include_once($config["base_path"]."/lib/api_device.php");
41 include_once($config["base_path"]."/lib/api_automation_tools.php");
42
43 /*
44 include_once($config["base_path"]."/lib/api_data_source.php");
45 include_once($config["base_path"]."/lib/api_graph.php");
46 include_once($config["base_path"]."/lib/functions.php");
47 */
48
49 /* process calling arguments */
50 $action = '';
51 $ip = '';
52 $host_template = '';
53 // $delete_graphs = FALSE;
54 $parms = $_SERVER["argv"];
55 array_shift($parms);
56 if (sizeof($parms)) {
57         foreach($parms as $parameter) {
58                 @list($arg, $value) = @explode("=", $parameter);
59                 switch ($arg) {
60         case "--get-graphs":
61                         $action = 'get-graphs';
62             break;
63         case "--drop-device":
64                         $action = 'drop-device';
65             break;
66 /*
67         case "--get-device":
68                         $action = 'get-device';
69             break;
70         case "--get-graph-templates":
71                         $action = 'get-graph-templates';
72             break;
73 */
74                 case "--ip":
75                         $ip = trim($value);
76                         break;
77                 case "--host-template":
78                         $host_template = trim($value);
79                         break;
80 /*
81                 case "--delete-graphs":
82                         $delete_graphs = TRUE;
83                         break;
84 */
85                 case "--version":
86                 case "-V":
87                 case "-H":
88                 case "--help":
89                         die(default_die());
90                 default:
91                         die("ERROR: Invalid Argument: ($arg)");
92                 }
93         }
94 } else {
95   die(default_die());
96 }
97
98 /* Now take an action */
99 switch ($action) {
100 case "get-graphs":
101         displayHostGraphs(host_id($ip),TRUE);
102         break;
103 case "drop-device":
104         $host_id = host_id($ip);
105 /*
106         if ($delete_graphs) {
107                 // code copied & pasted from version 0.8.8a
108         // cacti/site/lib/host.php and cacti/site/graphs.php 
109                 // unfortunately no api function for this yet
110                 $graphs = db_fetch_assoc("select
111                         graph_local.id as local_graph_id
112                         from graph_local
113                         where graph_local.host_id=" . $host_id);
114                 if (sizeof($graphs) > 0) {
115                         foreach ($graphs as $graph) {
116                                 $data_sources = array_rekey(db_fetch_assoc("SELECT data_template_data.local_data_id
117                                         FROM (data_template_rrd, data_template_data, graph_templates_item)
118                                         WHERE graph_templates_item.task_item_id=data_template_rrd.id
119                                         AND data_template_rrd.local_data_id=data_template_data.local_data_id
120                                         AND graph_templates_item.local_graph_id=" . $graph["local_graph_id"] . "
121                                         AND data_template_data.local_data_id > 0"), "local_data_id", "local_data_id");
122                                 if (sizeof($data_sources)) {
123                                         api_data_source_remove_multi($data_sources);
124                                 }
125                                 api_graph_remove($graph["local_graph_id"]);
126                         }
127                 }
128         }
129 */
130         api_device_remove($host_id);
131         if (host_id($ip,1)) {
132                 die("Failed to remove hostname $ip");
133         }
134         exit(0);
135 /*
136 case "get-device":
137         echo host_id($ip);
138         exit(0);
139 case "get-graph-templates":
140         if (!$host_template) {
141                 die("No host template specified");
142         }
143         $graphs = getGraphTemplatesByHostTemplate($host_template);
144         if (sizeof($graphs)) {
145                 foreach (array_keys($graphs) as $gtid) {
146                         echo $gtid . "\n";
147                 }
148                 exit(0);
149         }
150         die("No graph templates associated with this host template");
151 */
152 default:
153         die("Specified action not found, contact a developer");
154 }
155
156 function default_die() {
157   return "Cacti interface for freeside.  Do not use for anything else.";
158 }
159
160 function host_id($ip_address, $nodie=0) {
161         if (!$ip_address) {
162                 die("No hostname specified");
163         }
164         $devices = array();
165         $query = "select id from host";
166         $query .= " where hostname='$ip_address'";
167         $devices = db_fetch_assoc($query);
168         if (sizeof($devices) > 1) {
169         // This should never happen, just being thorough
170                 die("Multiple devices found for hostname $ip_address");
171         } else if (!sizeof($devices)) {
172                 if ($nodie) {
173                         return '';
174                 } else {
175                         die("Could not find hostname $ip_address");
176                 }
177         }
178         return $devices[0]['id'];
179 }
180
181 ?>