Asterisk Query

Post support questions that directly relate to Linux/Unix operating systems.

Moderators: Developers, Moderators

Post Reply
User avatar
oppossum
Posts: 6
Joined: Thu Jan 13, 2011 1:55 pm
Location: Atlanta

Asterisk Query

Post by oppossum »

I am having a similar problem. I wanted something that would return my call data from and asterisk box. I only want the Extension, and the call duration from a mysql file on that machine. What am I missing, any help would be welcomed at it may keep me from beating my head on the keyboard continuously!

Code: Select all

<?PHP

$user_name = "user";
$password = "pass";
$database = "mysqldb";
$server = "1.1.1.1:3306";

	$db_handle = mysql_connect($server, $user_name, $password);

	$db_found = mysql_select_db($database, $db_handle);

	if ($db_found) {

		$SQL = "SELECT dst,duration FROM cdr WHERE calldate > NOW() - INTERVAL 1 MINUTE";
		$result = mysql_query($SQL);

	while ($db_field = mysql_fetch_array($result)) {
		
		print"Ext-";
		print $db_field['dst']."";
		print":";
		print $db_field['duration']." ";

	}

		mysql_close($db_handle);

}
	else{
		print"fail.";
		mysql_close($db_handle);
}

?>
Any help / suggestions are great,
Thanks!
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Asterisk Query

Post by gandalf »

Topic split off as it tackles a very different problem.
Please change the script in a way to spit out the whole line of output in a single print statement.
R.
User avatar
oppossum
Posts: 6
Joined: Thu Jan 13, 2011 1:55 pm
Location: Atlanta

Re: Asterisk Query

Post by oppossum »

Thanks for the help! This is what I came up with. Now after I add the data (as a script/command) and graph template, create my graph it shows the normal "RRD Does not Exist" (excitement :D ) for 1 minute then I get the broken picture icon (head desk bang :( ). Again, any help is great. I am struggling to understand and hope it gets easier as I learn.

Code: Select all

<?PHP

//login
$user_name = "cacti";
$password = "cacti";
$database = "asteriskcdrdb";
$server = "1.1.1.1:3306";

	$db_handle = mysql_connect($server, $user_name, $password);

	$db_found = mysql_select_db($database, $db_handle);


	if ($db_found) {



		$SQL = "SELECT dst,duration FROM cdr WHERE calldate > NOW() - INTERVAL 1 MINUTE";
		$result = mysql_query($SQL);

	while ($db_field = mysql_fetch_array($result)) {
		
		print"Ext- {$db_field['dst']}:{$db_field['duration']} ";
	}

		mysql_close($db_handle);

}
	else{
		print"fail.";
		mysql_close($db_handle);
}

?>
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Asterisk Query

Post by gandalf »

Please show us a line of output from your script.
R.
User avatar
oppossum
Posts: 6
Joined: Thu Jan 13, 2011 1:55 pm
Location: Atlanta

Re: Asterisk Query

Post by oppossum »

You got it:

Code: Select all

[root@cacti scripts]# php get_mysql_data.php
Ext- 89:0 Ext- 68:0 Ext- 77:0 Ext- 87:0 Ext- 87:0 Ext- 89:0 Ext- 77:0 Ext- 68:0 Ext- 11111111111:2 Ext- 59:0 Ext- 59:0 Ext- 68:0 Ext- 87:0 Ext- 89:0 Ext- 77:0 Ext- 11111111111:0 Ext- 2000:37 [root@cacti scripts]#
It is only pulling info for the last minute. I dont want or need any Ext- over 3 digits. I have added a return""; line under the print""; that I saw someone else using in their code hoping that would fix it. (no luck there). The 11111111111 are edited client phone numbers.

If this was the return I got back to cacti, I would not want to graph any of this run. The Ext- XX:0's I dont need to know they were on for 0 seconds, and the Ext- 2000:37 is a queue. Maybe nice in the future to have that data but not necessary yet.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Asterisk Query

Post by gandalf »

Please do not use whitespace for the output denominators. Use e.g. Ext-2000:1234 instead of Ext- 2000:1234
R.
User avatar
oppossum
Posts: 6
Joined: Thu Jan 13, 2011 1:55 pm
Location: Atlanta

Re: Asterisk Query

Post by oppossum »

new output:

Code: Select all

[root@cacti scripts]# php get_mysql_data.php
Ext-89:1 [root@cacti scripts]#
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Asterisk Query

Post by gandalf »

And, does it help?
R.
User avatar
oppossum
Posts: 6
Joined: Thu Jan 13, 2011 1:55 pm
Location: Atlanta

Re: Asterisk Query

Post by oppossum »

I am afraid not, I get the dreaded broken picture link on my graph page. Will continue to debug and see what I can figure out. Here is what the php looks like now.

Code: Select all

<?PHP

//login
$user_name = "cacti";
$password = "cacti";
$database = "asteriskcdrdb";
$server = "1.1.1.1:3306";

	$db_handle = mysql_connect($server, $user_name, $password);

	$db_found = mysql_select_db($database, $db_handle);


	if ($db_found) {

		$SQL = "SELECT * FROM cdr WHERE calldate > NOW() - INTERVAL 1 MINUTE";

		$result = mysql_query($SQL);

	while ($db_field = mysql_fetch_array($result)) {
		
		print"Ext-{$db_field['dst']}:{$db_field['duration']} ";
		return"Ext-{$db_field['dst']}:{$db_field['duration']} ";

	}

		mysql_close($db_handle);

}
	else{
		print"fail.";
		mysql_close($db_handle);
}

?>
There is a space btwn each of the outputs, is that correct?
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Asterisk Query

Post by gandalf »

Yep, space between outputs is required. But depending on your version of spine, trailing space will create problems
R.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests