Insert value of vmstat to database.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert value of vmstat to database.
# 1  
Old 08-17-2010
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

# 2  
Old 08-18-2010
Code:
$ vmstat 1 10 |awk -v s=39 '
BEGIN{ print "create table doctortux_vmstat_cpu(cpu_user number(3),cpu_system number(3),cpu_idle number(3))"
       print "statement to insert value to the table."}
NR>2 {printf "insert into doctortux_vmstat_cpu values(%c%d%c,%c%d%c,%c%d%c);\n",s,$13,s,s,$14,s,s,$15,s}'

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('4','1','4');
insert into doctortux_vmstat_cpu values('2','0','2');
insert into doctortux_vmstat_cpu values('7','0','7');
...

# 3  
Old 08-18-2010
Quote:
Originally Posted by rdcwayx
Code:
$ vmstat 1 10 |awk -v s=39 '
BEGIN{ print "create table doctortux_vmstat_cpu(cpu_user number(3),cpu_system number(3),cpu_idle number(3))"
       print "statement to insert value to the table."}
NR>2 {printf "insert into doctortux_vmstat_cpu values(%c%d%c,%c%d%c,%c%d%c);\n",s,$13,s,s,$14,s,s,$15,s}'

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('4','1','4');
insert into doctortux_vmstat_cpu values('2','0','2');
insert into doctortux_vmstat_cpu values('7','0','7');
...

Being novice linux user i have difficulties understanding following section.
why the variable s is initialized with 39 values.
what does NR>2 do?
I guess this will only generate the statement but wont insert them into database.
M i correct?
Any way thanks for spending time to give me a reply it was indeed helpful.
# 4  
Old 08-18-2010
39 is used to print ' in awk.
NR>2 is used to ignore the first two lines of vmware O/P.

if you need run the output, you can add |sh after that.

Code:
$ vmstat 1 10 |awk -v s=39 '
BEGIN{ print "create table doctortux_vmstat_cpu(cpu_user number(3),cpu_system number(3),cpu_idle number(3))"
       print "statement to insert value to the table."}
NR>2 {printf "insert into doctortux_vmstat_cpu values(%c%d%c,%c%d%c,%c%d%c);\n",s,$13,s,s,$14,s,s,$15,s}' |sh

# 5  
Old 08-18-2010
Quote:
Originally Posted by rdcwayx
39 is used to print ' in awk.
NR>2 is used to ignore the first two lines of vmware O/P.

if you need run the output, you can add |sh after that.

Code:
$ vmstat 1 10 |awk -v s=39 '
BEGIN{ print "create table doctortux_vmstat_cpu(cpu_user number(3),cpu_system number(3),cpu_idle number(3))"
       print "statement to insert value to the table."}
NR>2 {printf "insert into doctortux_vmstat_cpu values(%c%d%c,%c%d%c,%c%d%c);\n",s,$13,s,s,$14,s,s,$15,s}' |sh

I might not able to execute the command in this way.
as for executing sql command i have to write following code
How would i do this.
Code:
# 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse log file to insert into database

I have a log file that's created daily by this command: sar -u 300 288 >> /var/log/usage/$(date "+%Y-%m-%d")_$(hostname)_cpu.log It that contains data like this: Linux 3.16.0-4-amd64 (myhostname) 08/15/2015 _x86_64_ (1 CPU) 11:34:17 PM CPU %user %nice ... (12 Replies)
Discussion started by: unplugme71
12 Replies

2. Shell Programming and Scripting

Intelligent Script to Insert Records in Database Table

Hello All Members, I am new to this forum and to the shell scripting. I want to write a shell script to do the following: Scenario: I have a pipe delimited .txt file with multiple fields in it. The very first row of the file contains the name of the column which resembles the name of the... (18 Replies)
Discussion started by: chetanojha
18 Replies

3. Programming

Need help on Insert data to mySQL database

Hi guys, I would like to seek help on inserting data whenever the switch is on or off to my sensor mySQL database in phpMyAdmin from my control.php. I'm using Raspberry PI as my hardware and follow a few tutorials to create my own Web Control Interface, it works perfectly without insert method.... (1 Reply)
Discussion started by: aoiregion
1 Replies

4. Shell Programming and Scripting

Shell script for insert multiple records into a Database

I have a table in an Informix DB into which I want to insert multiple records at a time. Data for one of the column should be unique & other column data may be the same for all the records I insert Typical Insert Statement I use to insert one row : insert into employee(empid, country, state)... (5 Replies)
Discussion started by: nisav
5 Replies

5. Programming

Oracle Database: INSERT INTO with TO_DATE function

Hello, I am writting a procedure in shell script as, set serveroutput on declare date_gen DATE := $DATEGEN; begin INSERT INTO TBL1 ( EMPNAME,EMPID,EMPBDATE) VALUES($EMPNAME,$EMPID,TO_DATE(date_gen,'YYYY-MM-DD')); end; / Where DATEGEN is unix string variable which I need to use into... (10 Replies)
Discussion started by: Poonamol
10 Replies

6. Web Development

INSERT data to a Database Table from a text file

If you have a text file and if you want to Insert data to your Database Table, You can do it with these queries LOAD DATA LOCAL INFILE '/path/yourTextFile.txt' INTO TABLE yourTableName FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' (0 Replies)
Discussion started by: sitex
0 Replies

7. Shell Programming and Scripting

insert values into sqlplus database using shell script

hello all, I am new to shell scripting and to unix... so the following is my assignment.. here i am trying to insert a values into sqlplus database using shell script. the following is my shell script InsertDelete.sh #! /bin/sh echo "*********The MENU******** 1.Insert The Values... (2 Replies)
Discussion started by: pradeept
2 Replies

8. Shell Programming and Scripting

how to insert data into database by reading it from a text file??

Hi....can you guys help me out in this script?? Below is a text file and it contains these: GEF001 000093625 MKL002510 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL003604 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL005675 000001... (4 Replies)
Discussion started by: pallavishetty
4 Replies

9. Shell Programming and Scripting

How to insert data into MYSql database from a text file

Hi, Need to get help from you guys about this issue. I need to insert data into MySql database from a text file which is located in other server. The text file is something look like below: Date | SubscriberNo | Call Duration 20/7/07 | 123456788 | 20 20/7/07 | 123412344 | 30 The... (4 Replies)
Discussion started by: shirleyeow
4 Replies

10. Shell Programming and Scripting

how to insert data in database based on text file?

Hi....can you guys help me out in this script?? Below is a text file script....called Bukom.txt and it contains these: BUKOM 20060101 2.5 2.6 2.7 2.8 2.9 2.3 2.1 BUKOM 20060102 2.4 2.5 2.6 2.7 2.7 2.6 2.4 BUKOM 20060103 2.1 ... (9 Replies)
Discussion started by: forevercalz
9 Replies
Login or Register to Ask a Question