[OpenSIPS-Devel] osipsconsole output to stderr

Brendan Sterne brendan at callvine.com
Sat May 8 19:36:01 CEST 2010


Hi Bogdan,

Thanks for all your excellent work on the OpenSIPS project.

I have included a patch for 'osipsconsole' below.  It addresses 4  
things:
1) Use perl's glob operator <> to expand the ~ when looking  
for .osipsconsolerc (wasn't working before)
2) Redirect sterr to /dev/null whenever the 'which' command is called  
(avoids unnecessary stderr output)
3) Fix the 'gateway add' command so that it parses the args properly  
(typos -> some cmd[2] should be var[0])
4) Fix the 'dr' command output by pulling out mysql results using the  
column names (instead of position-based)

I am automating some OpenSIPS dynamic routing configuration by calling  
osipsconsole inside other scripts, that is how I discovered the  
existing issues, and how I have tested these changes.

Let me know if there is any other info you would like.

Cheers,
- Brendan


Index: /Users/brendansterne/Projects/EclipseWorkspace/opensip trunk/ 
scripts/osipsconsole
===================================================================
--- /Users/brendansterne/Projects/EclipseWorkspace/opensip trunk/ 
scripts/osipsconsole	(revision 6848)
+++ /Users/brendansterne/Projects/EclipseWorkspace/opensip trunk/ 
scripts/osipsconsole	(working copy)
@@ -23,6 +23,7 @@
  # History:
  # -------
  #  2008-10-10  first version (iulia_bublea)
+#  2010-05-08  fixes to handle updated dr schema (brendan sterne)
  #

  #use strict;
@@ -56,8 +57,8 @@
  }


-if ( -e "~/.osipsconsolerc" ) {
-	$OSIPSCONSOLERC = "~/.osipsconsolerc";
+if ( -e <~/.osipsconsolerc> ) {
+	$OSIPSCONSOLERC = <~/.osipsconsolerc>;
  	&include_osipsconsolerc();	
  }

@@ -586,7 +587,7 @@
  #berkeley db utility program that writes out db to plain text
  #small hack to autodetect the db dump command, debian prefix the  
version..

-system("which db_dump > /dev/null");
+system("which db_dump > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$DUMP_CMD = "db_dump";					
  }
@@ -591,7 +592,7 @@
  	$DUMP_CMD = "db_dump";					
  }

-system("which db4.4_dump > /dev/null");
+system("which db4.4_dump > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$DUMP_CMD = "db4.4_dump";
  }
@@ -596,7 +597,7 @@
  	$DUMP_CMD = "db4.4_dump";
  }

-system("which db4.5_dump > /dev/null");
+system("which db4.5_dump > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$DUMP_CMD = "db4.5_dump";
  }
@@ -601,7 +602,7 @@
  	$DUMP_CMD = "db4.5_dump";
  }

-system("which db4.6_dump > /dev/null");
+system("which db4.6_dump > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$DUMP_CMD = "db4.6_dump";
  }
@@ -609,7 +610,7 @@
  #berkeley db utility program that imports data from plain text file
  #small hack to autodetect the db load command, debian prefix the  
version..

-system("which db_load > /dev/null");
+system("which db_load > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$LOAD_CMD = "db_load";
  }
@@ -614,7 +615,7 @@
  	$LOAD_CMD = "db_load";
  }

-system("which db4.4_load > /dev/null");
+system("which db4.4_load > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$LOAD_CMD = "db4.4_load";
  }
@@ -619,7 +620,7 @@
  	$LOAD_CMD = "db4.4_load";
  }

-system("which db4.5_load > /dev/null");
+system("which db4.5_load > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$LOAD_CMD = "db4.5_load";
  }
@@ -624,7 +625,7 @@
  	$LOAD_CMD = "db4.5_load";
  }

-system("which db4.6_load > /dev/null");
+system("which db4.6_load > /dev/null 2>&1");
  if ( $? == 0 ) {
  	$LOAD_CMD = "db4.6_load";
  }
@@ -887,7 +888,7 @@
  sub locate_tool() {
          while ( 1 ){
                 if ( -x "/usr/bin/which" ) {
-                        $TOOLPATH = `which @_`;
+                        $TOOLPATH = `which @_ 2> /dev/null`;
  			chomp($TOOLPATH);
                          #if ( $TOOLPATH ) {
                          #       return $TOOLPATH;
@@ -6838,7 +6839,7 @@
  	if ( $PW eq "" ) {
  		$X = system("mysql -h $DBHOST -u$DBROOTUSER -e \"INSERT INTO  
$dst_table ($dst_cols) SELECT $src_cols FROM $src_table\"");
  		if ( $? != 0 ) {
-			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null");
+			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null 2>&1");
  			if ( $? != 0 ) {
  				print " -- Migrating $_[2] to $_[0].....SKIPPED (no source)\n";
  				return;
@@ -6852,7 +6853,7 @@
  	} else {
  		$X = system("mysql -h $DBHOST -u$DBROOTUSER -p$PW -e \"INSERT INTO  
$dst_table ($dst_cols) SELECT $src_cols FROM $src_table\"");
  		if ( $? != 0 ) {
-			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null");
+			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null 2>&1");
  			if ( $? != 0 ) {
  				print " -- Migrating $_[2] to $_[0].....SKIPPED (no source)\n";
  				return;
@@ -7630,16 +7631,16 @@
  					$i += 4;
  					while ( $i < $#cmd+1 ) {
  						@var = split ("=",$cmd[$i]);
-							if ($var[0] =~ /type/) {
+							if ($var[0] =~ /^type$/) {
  								$type = $var[1];
  							}
-							elsif ($cmd[2] =~ /strip/) {
+							elsif ($var[0] =~ /^strip$/) {
  								$strip = $var[1];
  							}
-							elsif ($cmd[2] =~ /pri_prefix/) {
+							elsif ($var[0] =~ /^pri_prefix$/) {
  								$pri_prefix = $var[1];
  							}
-							elsif ($cmd[2] =~ /description/) {
+							elsif ($var[0] =~ /^description$/) {
  								$description = $var[1];
  							}
  						$i++;
@@ -7710,9 +7711,14 @@
  	
  						#Retrieve the results of a row of data and print
  						print "\tQuery results: 
\n================================================\n";
-						while ( @row = $sth->fetchrow_array( ) )  {
-							print "gwid=$row[0] type=$row[1] address=$row[2] strip=$row[3]  
pri_prefix=$row[4] description=$row[5]\n";
-						}	
+						while ( my %row = %{ $sth->fetchrow_hashref() } ) {
+							print "gwid=$row{$dr_gw_table{'DR_GW_GWID_COLUMN'}} " .
+								"type=$row{$dr_gw_table{'DR_GW_TYPE_COLUMN'}} " .
+								"address=$row{$dr_gw_table{'DR_GW_ADDRESS_COLUMN'}} " .
+								"strip=$row{$dr_gw_table{'DR_GW_STRIP_COLUMN'}} " .
+								"pri_prefix=$row{$dr_gw_table{'DR_GW_PRI_PREFIX_COLUMN'}} " .
+								"description=$row{$dr_gw_table{'DR_GW_DESCRIPTION_COLUMN'}}\n";
+						}
  						$sth->finish();
  					}	
  					return;	
@@ -7738,10 +7744,14 @@
  		
  							#Retrieve the results of a row of data and print
  							print "\tQuery results: 
\n================================================\n";
-							while ( @row = $sth->fetchrow_array( ) )  {
-								print "gwid=$row[0] type=$row[1] address=$row[2] strip=$row 
[3] ".
-								      " pri_prefix=$row[4] description=$row[5]\n";
-							}	
+							while ( my %row = %{ $sth->fetchrow_hashref() } ) {
+								print "gwid=$row{$dr_gw_table{'DR_GW_GWID_COLUMN'}} " .
+									"type=$row{$dr_gw_table{'DR_GW_TYPE_COLUMN'}} " .
+									"address=$row{$dr_gw_table{'DR_GW_ADDRESS_COLUMN'}} " .
+									"strip=$row{$dr_gw_table{'DR_GW_STRIP_COLUMN'}} " .
+									"pri_prefix=$row{$dr_gw_table{'DR_GW_PRI_PREFIX_COLUMN'}} " .
+									"description=$row{$dr_gw_table{'DR_GW_DESCRIPTION_COLUMN'}} 
\n";
+							}
  							$sth->finish();
  						}		
  					}
@@ -7763,11 +7773,15 @@
  		
  						    #Retrieve the results of a row of data and print
  						    print "\tQuery results: 
\n================================================\n";
-						    while ( @row = $sth->fetchrow_array( ) )  {
-						    	print "gwid=$row[0] type=$row[1] address=$row[2] strip=$row 
[3] ".
-							      " pri_prefix=$row[4] description=$row[5]\n";
-							}	
-						$sth->finish();
+							while ( my %row = %{ $sth->fetchrow_hashref() } ) {
+								print "gwid=$row{$dr_gw_table{'DR_GW_GWID_COLUMN'}} " .
+									"type=$row{$dr_gw_table{'DR_GW_TYPE_COLUMN'}} " .
+									"address=$row{$dr_gw_table{'DR_GW_ADDRESS_COLUMN'}} " .
+									"strip=$row{$dr_gw_table{'DR_GW_STRIP_COLUMN'}} " .
+									"pri_prefix=$row{$dr_gw_table{'DR_GW_PRI_PREFIX_COLUMN'}} " .
+									"description=$row{$dr_gw_table{'DR_GW_DESCRIPTION_COLUMN'}} 
\n";
+							}
+							$sth->finish();
  						}		
  					}
  				}
@@ -7895,9 +7909,17 @@

  							#Retrieve the results of a row of data and print
  							print "\tQuery results: 
\n================================================\n";
-							while ( @row = $sth->fetchrow_array( ) )  {
-								print "ruleid=$row[0] groupid=$row[1] prefix=$row[2] timerec= 
$row[3] priority=$row[4] routeid=$row[5] gwlist=$row[6] description= 
$row[7]\n";
-						}	
+							while ( my %row = %{ $sth->fetchrow_hashref() } ) {
+								print "ruleid=$row{$dr_rules_table{'DR_RULES_RULEID_COLUMN'}}  
" .
+									"groupid=$row{$dr_rules_table{'DR_RULES_GROUPID_COLUMN'}} " .
+									"prefix=$row{$dr_rules_table{'DR_RULES_PREFIX_COLUMN'}} " .
+									"timerec=$row{$dr_rules_table{'DR_RULES_TIMERES_COLUMN'}} " .
+									"priority=$row{$dr_rules_table{'DR_RULES_PRIORITY_COLUMN'}}  
" .
+									"routeid=$row{$dr_rules_table{'DR_RULES_ROUTEID_COLUMN'}} " .
+									"gwlist=$row{$dr_rules_table{'DR_RULES_GWLIST_COLUMN'}} " .
+									"description=$row{$dr_rules_table 
{'DR_RULES_DESCRIPTION_COLUMN'}} " .
+									"\n";
+							}
  							$sth->finish();
  						}	
  						return;	
@@ -7926,11 +7948,17 @@
  		
  									#Retrieve the results of a row of data and print
  									print "\tQuery results: 
\n================================================\n";
-									while ( @row = $sth->fetchrow_array( ) )  {
-										print "ruleid=$row[0] groupid=$row[1] prefix=$row[2] ".
-										      "timerec=$row[3] priority=$row[4] routeid=$row[5] ".
-										      "gwlist=$row[6] description=$row[7]\n";
-									}	
+									while ( my %row = %{ $sth->fetchrow_hashref() } ) {
+									print "ruleid=$row{$dr_rules_table 
{'DR_RULES_RULEID_COLUMN'}} " .
+										"groupid=$row{$dr_rules_table{'DR_RULES_GROUPID_COLUMN'}} " .
+										"prefix=$row{$dr_rules_table{'DR_RULES_PREFIX_COLUMN'}} " .
+										"timerec=$row{$dr_rules_table{'DR_RULES_TIMERES_COLUMN'}} " .
+										"priority=$row{$dr_rules_table{'DR_RULES_PRIORITY_COLUMN'}}  
" .
+										"routeid=$row{$dr_rules_table{'DR_RULES_ROUTEID_COLUMN'}} " .
+										"gwlist=$row{$dr_rules_table{'DR_RULES_GWLIST_COLUMN'}} " .
+										"description=$row{$dr_rules_table 
{'DR_RULES_DESCRIPTION_COLUMN'}} " .
+										"\n";
+									}
  									$sth->finish();
  								}		
  							}
@@ -7955,11 +7983,17 @@
  		
  									#Retrieve the results of a row of data and print
  									print "\tQuery results: 
\n================================================\n";
-									while ( @row = $sth->fetchrow_array( ) )  {
-										print "ruleid=$row[0] groupid=$row[1] prefix=$row[2] ".
-										      "timerec=$row[3] priority=$row[4] routeid=$row[5] ".
-										      "gwlist=$row[6] description=$row[7]\n";
-									}	
+									while ( my %row = %{ $sth->fetchrow_hashref() } ) {
+									print "ruleid=$row{$dr_rules_table 
{'DR_RULES_RULEID_COLUMN'}} " .
+										"groupid=$row{$dr_rules_table{'DR_RULES_GROUPID_COLUMN'}} " .
+										"prefix=$row{$dr_rules_table{'DR_RULES_PREFIX_COLUMN'}} " .
+										"timerec=$row{$dr_rules_table{'DR_RULES_TIMERES_COLUMN'}} " .
+										"priority=$row{$dr_rules_table{'DR_RULES_PRIORITY_COLUMN'}}  
" .
+										"routeid=$row{$dr_rules_table{'DR_RULES_ROUTEID_COLUMN'}} " .
+										"gwlist=$row{$dr_rules_table{'DR_RULES_GWLIST_COLUMN'}} " .
+										"description=$row{$dr_rules_table 
{'DR_RULES_DESCRIPTION_COLUMN'}} " .
+										"\n";
+									}
  									$sth->finish();
  								}		
  							}

Cheers,
- Brendan

Brendan Sterne
QA Lead, Callvine



On May 7, 2010, at 11:29 AM, Bogdan-Andrei Iancu wrote:

> Hi Brenden,
>
> it make sense - please send an unified patch for being uploaded on  
> SVN.
>
> Thanks and regards,
> Bogdan
>
> Brendan Sterne wrote:
>> Hi Bogdan,
>>
>> In these cases osipsconsole is just using 'which' to determine if an
>> executable exists.  The return code from 'which' is used.   If which
>> doesn't find the command it returns error and also dumps to stderr,
>> but osipsconsole doesn't care about the stderr text (and neither does
>> the user).
>>
>> For example:
>>
>> system("which db_dump > /dev/null 2>&1");
>> if ( $? == 0 ) {
>>     $DUMP_CMD = "db_dump";
>> }
>>
>> Also, I have found a few other issues:
>>
>> The ospipsconsole "dr gateway" command appears to be out-of-date with
>> regard to the dynamic routing mysql schema.  It is outputing fields  
>> by
>> row position dwid=$row[0] etc, but things don't line up any more
>> (specifically description is not being output).  I have fixed this in
>> my copy, and would like to share this fix.  How would I go about it?
>> Should I send a diff to you?
>>
>> Cheers,
>> - Brendan
>>
>> Brendan Sterne
>> QA Lead, Callvine
>>
>>
>>
>> On May 6, 2010, at 1:07 AM, Bogdan-Andrei Iancu wrote:
>>
>>
>>> Hi Brendan,
>>>
>>> Do you think it is a good idea to hide the errors ? I guess you  
>>> would
>>> like to know when something went wrong with the operation you are
>>> doing.
>>> The standard output is trashed as not important, but error
>>> notification/handling should be.
>>>
>>> What was the error in your case?
>>>
>>> Regards,
>>> Bogdan
>>>
>>> Brendan Sterne wrote:
>>>
>>>> Greetings,
>>>>
>>>> osipsconsole is printing to stderr when the 'which' command fails.
>>>> Although 'which' is being redirected to /dev/null, stderr is not
>>>> being
>>>> redirected.
>>>>
>>>> The fix I have made, is to also redirect stderr.   Attached is a
>>>> diff,
>>>> showing the fixes.  How do I go about getting this fix committed (I
>>>> am
>>>> new here)?
>>>>
>>>> < system("which db_dump > /dev/null");
>>>> ---
>>>>
>>>>> system("which db_dump > /dev/null 2>&1");
>>>>>
>>>> 594c596
>>>> < system("which db4.4_dump > /dev/null");
>>>> ---
>>>>
>>>>> system("which db4.4_dump > /dev/null 2>&1");
>>>>>
>>>> 599c601
>>>> < system("which db4.5_dump > /dev/null");
>>>> ---
>>>>
>>>>> system("which db4.5_dump > /dev/null 2>&1");
>>>>>
>>>> 604c606
>>>> < system("which db4.6_dump > /dev/null");
>>>> ---
>>>>
>>>>> system("which db4.6_dump > /dev/null 2>&1");
>>>>>
>>>> 612c614
>>>> < system("which db_load > /dev/null");
>>>> ---
>>>>
>>>>> system("which db_load > /dev/null 2>&1");
>>>>>
>>>> 617c619
>>>> < system("which db4.4_load > /dev/null");
>>>> ---
>>>>
>>>>> system("which db4.4_load > /dev/null 2>&1");
>>>>>
>>>> 622c624
>>>> < system("which db4.5_load > /dev/null");
>>>> ---
>>>>
>>>>> system("which db4.5_load > /dev/null 2>&1");
>>>>>
>>>> 627c629
>>>> < system("which db4.6_load > /dev/null");
>>>> ---
>>>>
>>>>> system("which db4.6_load > /dev/null 2>&1");
>>>>>
>>>> 890c892
>>>> <                         $TOOLPATH = `which @_`;
>>>> ---
>>>>
>>>>>                       $TOOLPATH = `which @_ 2> /dev/null`;
>>>>>
>>>> 6841c6843
>>>> < 			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null");
>>>> ---
>>>>
>>>>> 			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null 2>&1");
>>>>>
>>>> 6855c6857
>>>> < 			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null");
>>>> ---
>>>>
>>>>> 			system("echo $X | $EGREP \"ERROR 1146\" > /dev/null 2>&1");
>>>>>
>>>> Cheers,
>>>> - Brendan
>>>>
>>>> Brendan Sterne
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Devel mailing list
>>>> Devel at lists.opensips.org
>>>> http://lists.opensips.org/cgi-bin/mailman/listinfo/devel
>>>>
>>>>
>>>>
>>> -- 
>>> Bogdan-Andrei Iancu
>>> www.voice-system.ro
>>>
>>>
>>> _______________________________________________
>>> Devel mailing list
>>> Devel at lists.opensips.org
>>> http://lists.opensips.org/cgi-bin/mailman/listinfo/devel
>>>
>>
>>
>> _______________________________________________
>> Devel mailing list
>> Devel at lists.opensips.org
>> http://lists.opensips.org/cgi-bin/mailman/listinfo/devel
>>
>>
>
>
> -- 
> Bogdan-Andrei Iancu
> www.voice-system.ro
>
>
> _______________________________________________
> Devel mailing list
> Devel at lists.opensips.org
> http://lists.opensips.org/cgi-bin/mailman/listinfo/devel




More information about the Devel mailing list