How to write Perl Script to Get MQ Queue Depth?

Hi ,

I got the following script from internet to display queue depth using Perl Script. However, when I execute it , im getting following error. Can anyone shed light on what is going wrong?

 
 
#!/usr/bin/perl
## 07/23/01
## Depth Inquiry sample program. 
## Arguments: [Queue Name] [Queue Manager Name]
## Connects to the queue manager specified by the command line,
## if not supplied, it will connect to the default queue manager.
## It then inquires about the queue name passed in, specifically,
## the current depth of that queue. This is displayed to the user.
## This code can easily be modified to display other queue properties.
## Furthermore, MQINQ can be used to inquire about the attributes of
## other objects, not just queues.
##
## Author: Brandon Duncan
##         brandon@mqseries.net
##         www.mqseries.net
##
use MQSeries;
my $qname = $ARGV[0];
my $qmgrname = $ARGV[1];
my $Hconn = MQCONN($qmgrname, $CompCode, $Reason);
print"MQCONN reason:$Reason\n";
my $ObjDesc = { ObjectType => MQOT_Q, ObjectName => $qname };
my $Options = MQOO_INQUIRE | MQOO_SET | MQOO_FAIL_IF_QUIESCING;
my $Hobj = MQOPEN($Hconn,$ObjDesc,$Options,$CompCode,$Reason);
print"MQOPEN reason:$Reason\n";
my $tst = MQINQ($Hconn,$Hobj,$CompCode,$Reason,MQIA_CURRENT_Q_DEPTH);
print"Depth of $qname is: $tst\n";
MQCLOSE($Hconn,$Hobj,$COptions,$CompCode,$Reason);
print"MQCLOSE reason:$Reason\n";
MQDISC($Hconn,$CompCode,$Reason);
print"MQDISC reason:$Reason\n";

Exception:

 
bash-3.2$ perl mq.prg Queue1 Server1
 
Can't locate MQSeries.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at mq.prg line 19.
BEGIN failed--compilation aborted at mq.prg line 19.

The perl program is unable to find MQseries.pm in perl include path (@INC).

use lib "/path/to/yourmqseriesmodules/"
...<program code>...

Im sorry but I have little idea about perl modules.

Im assuming that we have to check for MQseries.pm module and get its path. How do I know whether it exists and how to find the path?

The Module is publicly published on the comprehensive perl archive network

try running (as root ideally)

$ cpan
...
cpan> install MQSeries
...
cpan> exit
$ perl mq.prg Queue1 Server1

It is giving the following error, repeated a no of times. I cant run as root. I don't have the permission :frowning:

 
System call "/usr/bin/wget -O - "ftp://ftp.perl.org/pub/CPAN/modules/03modlist.data.gz"  > /home/user1/.cpan/sources/modules/03modlist.data"
returned status 1 (wstat 256)
Warning: expected file [/home/user1/.cpan/sources/modules/03modlist.data.gz] doesn't exist
Issuing "/usr/bin/ftp -n"
ftp: ftp.perl.org: Name or service not known
Not connected.
Local directory now /home/user1/.cpan/sources/modules
Not connected.
Not connected.
Not connected.
Not connected.
Not connected.
Not connected.
Bad luck... Still failed!
Can't access URL ftp://ftp.perl.org/pub/CPAN/modules/03modlist.data.gz.
Please check, if the URLs I found in your configuration file () are valid.
The urllist can be edited. E.g. with 'o conf urllist push ftp://myurl/'
Could not fetch modules/03modlist.data.gz
Going to write /home/user1/.cpan/Metadata
Warning: Cannot install MQSeries, don't know what it is.
Try the command
    i /MQSeries/
to find objects with matching identifiers.
cpan> i /MQSeries/
No objects found of any type for argument /MQSeries/
 

You don't have access to the CPAN repository from your host?

You could take the required module code from these links
https://metacpan.org/source/MQSERIES/MQSeries-1.34/MQSeries.pm
https://metacpan.org/source/MQSeries::Config::Machine

and copy them into your PERL5LIB path (you may have to set up a PERL5LIB path in your environment eg. export PERL5LIB=${HOME}/Perl5/lib in ${HOME}/.profile)

Then create the files as
${PERL5LIB}/MQSeries.pm
and
${PERL5LIB}/MQSeries/Config/Machine.pm

Hi,

Thanks for all the help so far. Can you please let me know if the below process is fine.

(i) Create the directory - /home/user1/Perl5/lib

(ii) Put the files in

/home/user1/Perl5/lib/MQSeries.pm
/home/user1/Perl5/lib/MQSeries/Config/Machine.pm

(iii) Make a new entry in /home/user1/.profile as -
export PERL5LIB=${/home/user1}/Perl5/lib

---------- Post updated at 02:52 AM ---------- Previous update was at 02:25 AM ----------

I think I was mistaken. We have to put these files in /usr/lib/Perl5. I don't have permission to do that even. I have contacted MQ Team to check whether we can install MQ Series module via a change request.

If the files are present under /usr/lib/Perl5 Perl will use them, however if you haven't permissions to adjust the host's Perl libraries you can set your own with the PERL5LIB environment setting.

Note if you're using Bash ~/.bash_profile should export the setting.

Obviously adding the setting to your profile is insufficient in the current shell and the setting should be exported locally.

If you're running the command from an environment like cron where the profile is not read first you should call the script within a shell script such as

#!/bin/bash
export PERL5LIB=$PERL5LIB:/home/user1/Perl5/lib
/usr/bin/perl /path/to/mq.prg Queue1 Server1

Hi,

Thanks for all your help so far. I indeed have a bash shell and I ran the above command directly from bash shell that you mentioned after placing the PM files in /home/user1/Perl5/lib. However, it gave the Following error

 
export PERL5LIB=$PERL5LIB:/home/user1/Perl5/lib /usr/bin/perl /tmp/mq.prg Queue1 Server1
bash: export: `/usr/bin/perl': not a valid identifier
bash: export: `/tmp/mq.prg': not a valid identifier
bash: export: `Queue1': not a valid identifier
 

Sorry, my fault, you should not have spaces in the library path string, just ":" separators.

I set all the paths. Now im getting following error.

bash-3.2$ perl mq.prg Queue1 Server1
Undefined subroutine &main::MQCONN called at mq.prg line 24.

Hi skirmish,

I've found the full install tarball,
http://www.cpan.org/modules/by-authors/id/M/MQ/MQSERIES/MQSeries-1.34.tar.gz
This requires the mq sewrver be installed in order to create the Perl API. Check out the README.

Sorry for the delay in getting back.