Hi all,
while playing around with Nagvis, I noticed there was a plugin that would attempt to pass login credentials and graph info (graph id, height, width, etc) via php-curl calls in attempt to return a graph.
it didn't work and what I found was that the code would get stuck at the login page.
original code block is as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $sCactiUser.':'.$sCactiPass);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: image/png'));
curl_exec($ch);
curl_close($ch);
I found a few posts on the board related to php-curl and cacti indicating that use of cookies was what was required...
after more googl'ing... I tried the following:
! $url = 'http://<cactiserver address>/cacti/graph_image.php?' + the arguments passed on for the graph
! i.e. http://<cactiserver>/cacti/graph_image.php?action=view&local_graph_id=433&graph_height=50&graph_width=50&graph_nolegend=1
! $url2 = 'http://<cactiserver address>/cacti/'; used for initial login and getting the cookie...
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'login_username' => $sCactiUser,
'login_password' => $sCactiPass,
'realm' => 'local',
'action' => 'login'
)
);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec ($ch);
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: image/png'));
curl_exec ($ch);
curl_close ($ch);
However, this doesn't appear to be working. I do see the cooking file being generated/updated, but nothing after that.
no errors in the webserver logs, and I'm not sure what is missing...
does anyone have any ideas?
thanks
need help with php curl and cacti
Moderators: Developers, Moderators
-
- Cacti User
- Posts: 89
- Joined: Fri May 25, 2007 1:18 pm
-
- Cacti User
- Posts: 89
- Joined: Fri May 25, 2007 1:18 pm
Re: need help with php curl and cacti
to add:
I tried a simple test as follows:
<?php
/*
This script is an example of using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you.
If this script was a bit more advanced it might trick the server into
thinking its netscape and even pass a fake referer, yo look like it surfed
from a local page.
*/
header("Content-type: image/png");
$sCactiUser = 'guest';
$sCactiPass = 'guest';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://<cactiserver address>/cacti/graph_image.php?");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'login_username' => $sCactiUser,
'login_password' => $sCactiPass,
'realm' => 'local',
'action' => 'login'
)
);
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://<cactiserver address>/cacti/graph_image.php?action=view&local_graph_id=433&graph_height=100&graph_width=100&graph_nolegend=1");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
?>
ran this as test.php and all I get back is weird jumbled output... no image... cookiefile does get created...
I tried a simple test as follows:
<?php
/*
This script is an example of using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you.
If this script was a bit more advanced it might trick the server into
thinking its netscape and even pass a fake referer, yo look like it surfed
from a local page.
*/
header("Content-type: image/png");
$sCactiUser = 'guest';
$sCactiPass = 'guest';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://<cactiserver address>/cacti/graph_image.php?");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'login_username' => $sCactiUser,
'login_password' => $sCactiPass,
'realm' => 'local',
'action' => 'login'
)
);
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://<cactiserver address>/cacti/graph_image.php?action=view&local_graph_id=433&graph_height=100&graph_width=100&graph_nolegend=1");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
?>
ran this as test.php and all I get back is weird jumbled output... no image... cookiefile does get created...
-
- Cacti User
- Posts: 89
- Joined: Fri May 25, 2007 1:18 pm
Re: need help with php curl and cacti
! solved !
need to convert what comes back into an image.. sigh
so revised working code is now:
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'login_username' => $sCactiUser,
'login_password' => $sCactiPass,
'realm' => 'local',
'action' => 'login'
)
);
ob_start();
curl_exec ($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE,"/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_URL, $url);
$buf2 = curl_exec ($ch);
curl_close ($ch);
$imgRes = imagecreatefromstring($buf2);
imagejpeg($imgRes, NULL, 70);
need to convert what comes back into an image.. sigh
so revised working code is now:
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'login_username' => $sCactiUser,
'login_password' => $sCactiPass,
'realm' => 'local',
'action' => 'login'
)
);
ob_start();
curl_exec ($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE,"/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_URL, $url);
$buf2 = curl_exec ($ch);
curl_close ($ch);
$imgRes = imagecreatefromstring($buf2);
imagejpeg($imgRes, NULL, 70);
Re: need help with php curl and cacti
Hello, i presume this is the cacti graph plugin for nagvis you are referring to? Could you post the entire *working* code please? We are also experiencing problems with cacti graphs in nagvis. I set up everything the way it should be but it looks like the graph is not being loaded. I searched the entire web to get this working but with no results yet. Would love to hear from you soon!
Who is online
Users browsing this forum: No registered users and 5 guests