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