[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

read ps commands and put value into mysql (perl)

Posted on 2011-10-17
8
Medium Priority
?
239 Views
Last Modified: 2012-06-27
Hi
i would be really helpfull if any one can help me with my bellow requirment..

what i want is :

the perl script will read output from

ps aux | sort -nr -k 4 | head -12

so output will be :

mysql     1041  0.3  4.3 141588 22604 ?        Sl   09:15   1:32 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/       var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
www-data   904  0.0  2.2  41124 11660 ?        S    09:15   0:01 /usr/sbin/apache2 -k start
www-data  2739  0.0  2.2  41008 11344 ?        S    10:14   0:00 /usr/sbin/apache2 -k start
www-data  2774  0.0  2.0  39972 10308 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data   911  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   910  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   909  0.0  1.9  39972 10144 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   906  0.0  1.9  39712 10076 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data  2754  0.0  1.9  39972  9976 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data  2753  0.0  1.9  39716 10052 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data  2742  0.0  1.8  39716  9720 ?        S    10:14   0:00 /usr/sbin/apache2 -k start
root       827  0.0  1.7  37492  9128 ?        Ss   09:14   0:07 /usr/sbin/apache2 -k start


I want to insert colum 2, 3,4 and 11 into mysql database

mysql database entry would be like bellow

proces_number         ,cpu,           mem,        processname
1041                           0.3            4.3                  /usr/sbin/apache2 -k start


i know there are different way you can do

may be using split +hash ..

can any one please help to solve this issue ..

thanks for your advise..

0
Comment
Question by:fosiul01
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 36980494

my $dbh = DBI->connect("DBI:mysql:dbname:host", 'user', 'password',
                       { RaiseError => 1 }) or die $DBI::errstr;

my $sth = $dbh->prepare("insert into tablename(proces_number, cpu, mem, processname) values(?,?,?,?)");

while ( my $line = <DATA> ) {
    my ($proc, $cpu, $mem, $name) = (split(/\s+/, $line))[1,2,3,10];
    $sth->execute($proc, $cpu, $mem, $name);
}

__DATA__
mysql     1041  0.3  4.3 141588 22604 ?        Sl   09:15   1:32 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/       var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
www-data   904  0.0  2.2  41124 11660 ?        S    09:15   0:01 /usr/sbin/apache2 -k start
www-data  2739  0.0  2.2  41008 11344 ?        S    10:14   0:00 /usr/sbin/apache2 -k start
www-data  2774  0.0  2.0  39972 10308 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data   911  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   910  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   909  0.0  1.9  39972 10144 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   906  0.0  1.9  39712 10076 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data  2754  0.0  1.9  39972  9976 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data  2753  0.0  1.9  39716 10052 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data  2742  0.0  1.8  39716  9720 ?        S    10:14   0:00 /usr/sbin/apache2 -k start
root       827  0.0  1.7  37492  9128 ?        Ss   09:14   0:07 /usr/sbin/apache2 -k start

Open in new window

0
 
LVL 29

Author Comment

by:fosiul01
ID: 36980520
Hi thanks and great ...


i will try this now .. but instead of _DATA_

it output will be from bellow command

ps aux | sort -nr -k 4 | head -12

so instead of this

while ( my $line = <DATA> ) {


shall i type

my @output = `ps aux | sort -nr -k 4 | head -12 '


while ( my $line = $output  ) {

..........

thanks
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 36980543
No, you'd open the pipe to ps and use the same filehandle as you have in your original question. i.e.,
while (my $line = <PS_O>){

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:FishMonger
ID: 36980554
However, it's better to use a lexical var for the filehandle instead of the bareword.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 2000 total points
ID: 36983288
Here's the complete (untested) script.

#!/usr/bin/perl

use warnings;
use strict;
use DBI;

open (my $ps, '-|', "ps aux | sort -nr -k 4 | head -12")
  or die "Failed to open pipe to ps $!";

my $dbh = DBI->connect("DBI:mysql:dbname;host", 'user', 'password',
                       { RaiseError => 1 }) or die $DBI::errstr;

my $sth = $dbh->prepare("insert into tablename(proces_number,
                                               cpu,
                                               mem,
                                               processname)
                        values(?,?,?,?)"
                        );

while ( my $line = <$ps> ) {
    my ($proc, $cpu, $mem, $name) = (split(/\s+/, $line))[1,2,3,10];
    $sth->execute($proc, $cpu, $mem, $name);
}

$dbh->disconnect;
close $ps;

Open in new window

0
 
LVL 29

Author Comment

by:fosiul01
ID: 36984413
thanks man.. and sorry for late reply due to night ..
i will use this now and willcome back to you soon
0
 
LVL 29

Author Comment

by:fosiul01
ID: 36992056
HI

could you please explain about this bellow line

$sth->execute($proc, $cpu, $mem, $name);

and
my $sth = $dbh->prepare("insert into tablename(proces_number,
                                               cpu,
                                               mem,
                                               processname)
                        values(?,?,?,?)"
                        );



so whth

$sth->execute($proc, $cpu, $mem, $name);

you actually.. calling $sth variable of "my $sth = $dbh->prepare" ???



0
 
LVL 28

Expert Comment

by:FishMonger
ID: 36992784
The prepare statement is using placeholders (the 4 ? question marks) to represent the insert values and is being done outside of the loop so that it only needs to be prepared once, which is more efficient than doing the prepare for each iteration of the loop.  The values to be inserted are passed in the execute statement and the execute method automatically sql quotes those vars as needed.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question