read ps commands and put value into mysql (perl)

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..

LVL 29
fosiul01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:

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
fosiul01Author Commented:
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
FishMongerCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

FishMongerCommented:
However, it's better to use a lexical var for the filehandle instead of the bareword.
0
FishMongerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fosiul01Author Commented:
thanks man.. and sorry for late reply due to night ..
i will use this now and willcome back to you soon
0
fosiul01Author Commented:
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
FishMongerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.