LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2010, 06:27 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Insert value of vmstat to database.


Hi guys ,

I m trying to store the output of vmstat 1 10 into a database.

I have done the necessary homework to connect bash to interact with the database.
Program logic.
1)storing the output of vmstat 1 10 to a file named abc.txt
2)I m applying a filter to vmstat to get desired output containing cpu(user),cpu(system),cpu(idle) values.
3)
Code:
# cat abc.txt | grep -vE '(procs|free)'| awk '{print $13 " "$14 " " $15}'
0 1 97
0 0 100
0 1 99
0 2 94
0 1 90
0 0 100
0 0 100
0 0 100
1 1 90
0 1 96
My problem is how would i insert the values to the database.

I will have to include some sort of logic to read the output provided by
Code:
#cat abc.txt | grep -vE '(procs|free)'| awk '{print $13 " "$14 " " $15}'
and for every line i have to insert values to table.
Table :
Quote:
create table doctortux_vmstat_cpu(cpu_user number(3),cpu_system number(3),cpu_idle number(3))
statement to insert value to the table.
insert into doctortux_vmstat_cpu values('$CPU_USER','$CPU_SYSTEM','$CPU_IDLE');
The tough part is to assign values to bash variable CPU_USER,CPU_SYSTEM,CPU_IDLE.


script to connect to database(oracle xe).
Code:
#!/bin/bash
# Validate the value of ORACLE_HOME #
# If ORACLE_HOME is empty #

if [ -z $ORACLE_HOME ]
then
        echo "Set the ORACLE_HOME variable"
        exit 1
fi

# If ORACLE_HOME doesn't exist #
if [ ! -d $ORACLE_HOME ]
then
        echo "The ORACLE_HOME $ORACLE_HOME does not exist"
        exit 1
fi

# Validate the value of ORACLE_SID #
if [ -z $ORACLE_SID ]
then
        echo "Set the ORACLE_SID variable"
        exit 1
fi

SID=`ps -ef|grep smon|grep -v grep|awk '{print $8}'`

if [ 'xe_smon_'$ORACLE_SID != $SID ] ; then
echo "ORACLE_SID IS NOT SET"
exit 1
fi


# Enter the username and password to login to oracle #
echo "Enter the username"
read username

echo "Enter password"
stty -echo
read password
stty echo

# Get the query , no validation applied for query #
echo "Enter the query"
read query

# Login and execute the query.
echo "set feedback off verify off heading off pagesize 0
        $query
        exit" | $ORACLE_HOME/bin/sqlplus -s $username/$password | while read output ;
       do
                echo $output
        done
 
Old 08-18-2010, 01:06 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
If you store your numeric data
Code:
0 1 97
0 0 100
0 1 99
0 2 94
0 1 90
0 0 100
0 0
0 0 100
1 1 90
0 1 96
in a file eg nums.dat, you can generate sqlplus inserts
Code:
 awk   '{ print "INSERT INTO doctortux_vmstat_cpu (col1, col2, col3) values VALUES ("$1 "," $2 "," $3");" }' nums.dat | sqlplus ....
 
Old 08-19-2010, 12:04 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by chrism01 View Post
If you store your numeric data
Code:
0 1 97
0 0 100
0 1 99
0 2 94
0 1 90
0 0 100
0 0
0 0 100
1 1 90
0 1 96
in a file eg nums.dat, you can generate sqlplus inserts
Code:
 awk   '{ print "INSERT INTO doctortux_vmstat_cpu (col1, col2, col3) values VALUES ("$1 "," $2 "," $3");" }' nums.dat | sqlplus ....
Let me give it a chance.
 
Old 08-19-2010, 12:12 AM   #4
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by pinga123 View Post
Let me give it a chance.
can u please give me exact command to enter into database as i m not able to do it using following syntax.

Code:
(awk   '{ print "INSERT INTO doctortux_vmstat_cpu (col1, col2, col3) values VALUES ("$1 "," $2 "," $3");" }' nums.dat |sqlplus -s lightweight/oracle | while read output ;
       do
                echo $output
        done)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to insert value in database deepu_linux Programming 14 04-25-2013 01:07 PM
MySQL insert database problem macaronij Linux - Server 3 07-22-2010 04:38 AM
how to insert elements into a combobox from a database using NETBEANS Kakarot_Rathish Programming 1 04-27-2010 03:20 AM
using perl to insert special characters into mysql database ihopeto Programming 5 12-25-2006 05:08 PM
After insert data, Database is empty ivankong Linux - Software 2 06-16-2003 08:35 AM


All times are GMT -5. The time now is 07:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration