Activate disabled device with a script

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Post Reply
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Activate disabled device with a script

Post by jonathanross »

Hello,

I'm using EC2 Wowza and need to have a script update several servers at once so graphs are generated. By default the localhost profile is disabled.

Everything works fine with a script I've written to make a DB change to basically untick "disable" on host.php but I need to be able to then submit the form on that page ie click "save" to update other details over the command line. All the CLI scripts don't quite do it if I'm right.

I've tried everything I can think of.

Any experts willing to help ? It must be possible and I can run any MySQL or command line commands if needbe (such as php add_device.php).

Thanks !!

JR
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Activate disabled device with a script

Post by noname »

How about this?

To enable:
mysql> update host set disabled='' where id='xx';

To disable:
mysql> update host set disabled='on' where id='xx';

(See function form_actions() in host.php)
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Re: Activate disabled device with a script

Post by jonathanross »

Hi,

Thanks very much for the pointer.

I had this below but couldn't make head or tail of the PHP. It unticked the box but didn't set the changes live and graphs aren't generated.

Code: Select all

UPDATE host SET disabled ='no';
I've tried yours and it doesn't "submit" the form seemingly either unfortunately.

Can you make any more sense of the PHP function by chance ?

Thanks again,

JR
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Activate disabled device with a script

Post by noname »

?
Sorry, could you explain in detail what you want to do?

Do you want to setup new device from start via CLI? Otherwise, simply disabling/enabling the existing hosts?
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Re: Activate disabled device with a script

Post by jonathanross »

It's just enabling a disabled device.

I thought the SQL I had worked but the host.php page obviously submits something else to activate the graphs.

Thanks!
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Activate disabled device with a script

Post by noname »

Well, then how about this PHP code?

Code: Select all

% cat dev_avail.php

<?php

chdir('/var/www/cacti/');  # your Cacti's directory

include('./include/global.php');
include_once($config['library_path'] . '/database.php');

if (isset($_GET['enable'])) {
        $ids = explode(',', $_GET['enable']);
        for ($i = 0; ($i < count($ids)); $i++) {
                input_validate_input_number($ids[$i]);
                db_execute("update host set disabled='' where id='" . $ids[$i] . "'");
                echo "enabled:" . $ids[$i] . "<br>";
        }
}

if (isset($_GET['disable'])) {
        $ids = explode(',', $_GET['disable']);
        for ($i = 0; ($i < count($ids)); $i++) {
                input_validate_input_number($ids[$i]);
                db_execute("update host set disabled='on' where id='" . $ids[$i] . "'");
                echo "disabled:" . $ids[$i] . "<br>";
        }
}

?>
Call via browser as follows:
http://your_server/any_path/dev_avail.php?enable=1,2,3&disable=4,5,6
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Re: Activate disabled device with a script

Post by jonathanross »

I REALLY appreciate your help. Getting this to work would save me lots of clicking and unticking !

I can't quite get it working unfortunately.

The localhost id is 2 and I get "enabled:2" back from the script okay but it seems to just untick the box without setting the graphs live.

I also tried it through my script which would be ideal but I couldn't get PHP to execute it properly:

Code: Select all

PHPFILE=avail_dev.php
PHPDIR=/var/www/html/cacti
scp -i /root/.ssh/$1 -o 'StrictHostKeyChecking no' -p $PHPFILE ${x}:$PHPDIR
ssh -i /root/.ssh/$1 root@$x su root -c "\"php $PHPDIR/$PHPFILE?enable=2\""
I know I could use wget or similar in the script to trigger it if I can get the PHP going so it doesn't have to execute this way obviously.

Mmm, tricky ! So close yet so far :D

Thanks again,

JR
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Activate disabled device with a script

Post by noname »

jonathanross wrote: The localhost id is 2 and I get "enabled:2" back from the script okay but it seems to just untick the box without setting the graphs live.
Hmm, then try this code..

Added the code which updates poller cache (and including 'utility.php' for the purpose).
That is the same as the form_actions() do.

Code: Select all

% cat dev_avail.php

<?php

chdir('/var/www/cacti/');  # your Cacti's directory

include('./include/global.php');
include_once($config['library_path'] . '/database.php');
include_once($config['library_path'] . '/utility.php');

if (isset($_GET['enable'])) {
        $ids = explode(',', $_GET['enable']);
        for ($i = 0; ($i < count($ids)); $i++) {
                input_validate_input_number($ids[$i]);
                db_execute("update host set disabled='' where id='" . $ids[$i] . "'");

                /* update poller cache */
                $data_sources = db_fetch_assoc("select id from data_local where host_id='" . $ids[$i] . "'");
                if (sizeof($data_sources) > 0) {
                        foreach ($data_sources as $data_source) {
                                update_poller_cache($data_source["id"], false);
                        }
                }

                echo "enabled:" . $ids[$i] . "<br>";
        }
}

if (isset($_GET['disable'])) {
        $ids = explode(',', $_GET['disable']);
        for ($i = 0; ($i < count($ids)); $i++) {
                input_validate_input_number($ids[$i]);
                db_execute("update host set disabled='on' where id='" . $ids[$i] . "'");

                /* update poller cache */
                db_execute("delete from poller_item where host_id='" . $ids[$i] . "'");
                db_execute("delete from poller_reindex where host_id='" . $ids[$i] . "'");

                echo "disabled:" . $ids[$i] . "<br>";
        }
}

?>
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Re: Activate disabled device with a script

Post by jonathanross »

Thanks, I manually ran the poller script in crontab too.

I'll give this a shot in 30 mins.

Much appreciated :D
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Re: Activate disabled device with a script

Post by jonathanross »

WOW ! It's working.

Thanks very much :D

If there were points to award you'd win them all !

Thank you !
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Activate disabled device with a script

Post by noname »

Cheers :D
I'm so glad to hear that.
jonathanross
Posts: 7
Joined: Thu Aug 25, 2011 10:09 am

Re: Activate disabled device with a script

Post by jonathanross »

Can you tell I'm happy ?

:D Thanks again.
panvin
Posts: 3
Joined: Tue Aug 13, 2013 11:59 pm

Re: Activate disabled device with a script

Post by panvin »

Thank you guys so much for this script,

but I do not understand how to execute it from cmd line, if I try:
/usr/bin/php /usr/share/cacti/dev_avail.php?disable=644 I get an error "Could not open input file: /usr/share/cacti/dev_avail.php?disable=644"

Could you explain how to make it work from shell?

update:
wget --spider http://hostname/cacti/dev_avail.php?enable=$ids > /dev/null 2>&1
panvin
Posts: 3
Joined: Tue Aug 13, 2013 11:59 pm

Re: Activate disabled device with a script

Post by panvin »

there is addition for DELETE action:
if (isset($_GET['delete'])) {

if (!isset($_GET["delete_type"])) { $_GET["delete_type"] = 2; }

$ids = explode(',', $_GET['delete']);

$data_sources_to_act_on = array();
$graphs_to_act_on = array();
$devices_to_act_on = array();

for ($i=0; $i<count($ids); $i++) {
input_validate_input_number($ids[$i]);
$data_sources = db_fetch_assoc("select
data_local.id as local_data_id
from data_local
where " . array_to_sql_or($ids, "data_local.host_id"));
if (sizeof($data_sources) > 0) {
foreach ($data_sources as $data_source) {
$data_sources_to_act_on[] = $data_source["local_data_id"];
}
}

if ($_GET["delete_type"] == 2) {
$graphs = db_fetch_assoc("select
graph_local.id as local_graph_id
from graph_local
where " . array_to_sql_or($ids, "graph_local.host_id"));

if (sizeof($graphs) > 0) {
foreach ($graphs as $graph) {
$graphs_to_act_on[] = $graph["local_graph_id"];

}
}
}

$devices_to_act_on[] = $ids[$i];
echo "deleted:" . $ids[$i] . "<br>";
}

switch ($_GET["delete_type"]) {
case '1': /* leave graphs and data_sources in place, but disable the data sources */
api_data_source_disable_multi($data_sources_to_act_on);

break;
case '2': /* delete graphs/data sources tied to this device */
api_data_source_remove_multi($data_sources_to_act_on);

api_graph_remove_multi($graphs_to_act_on);

break;
}

api_device_remove_multi($devices_to_act_on);

}
syntax:
wget --spider http://hostname/cacti/dev_avail.php?del ... e_action=2 > /dev/null 2>&1

you can use delete_action 1 or 2, see comments in code.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests