Solved

Shell Script to insert into a database

Posted on 2011-03-15
2
547 Views
Last Modified: 2012-05-11
Hi,

I need a shell script that can insert the output of df -h every night into a database table. The table structure is shown below. Will appreciate any pointers on it

thanks
-anshu


[root@myserver ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              15G  4.0G  9.8G  29% /
/dev/sda6              27G  6.9G   19G  28% /omega
/dev/sda3             4.9G  390M  4.3G   9% /var
/dev/sda5             4.9G  139M  4.5G   3% /tmp
tmpfs                 127G     0  127G   0% /dev/shm
/dev/sdb2              35G   15G   19G  44% /alpha
/dev/sdc1             135G   28G  100G  22% /alpha1
/dev/sdd1             135G  105G   23G  83% /alpha2


Table Structure

SERVER_MACHINE | DATE_ID | MONTH_ID | YEAR_ID | Filesystem | Size_GB | Used_GB | Available_GB | USE_PCNT | Mounted On

myserver       | 20110315| 201103   | 2000    | /dev/sda1  | 15      |  4.0     | 9.8         | 29       | root
myserver       | 20110315| 201103   | 2000    | /dev/sda6  | 27      |  6.9     | 19.0        | 28       | omega


0
Comment
Question by:anshuma
  • 2
2 Comments
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35150403
give this a try assumes db is mysql but its generic sql

df -P >/tmp/dfout
server=`hostname`
d_date=`date +"%Y%m%d"`
m_date=`date +"%Y%m"`
y_date=`date +"%Y"`
toss_header=0
while read line
do
        if [ "$toss_header" -eq 0 ]
        then {
                toss_header=1
        } else {
                vol=`echo $line | awk '{print $1 }'`
                blocks=`echo $line | awk '{print $2 }'`
                used=`echo $line | awk '{print $3 }'`
                avail=`echo $line | awk '{print $4 }'`
                cap=`echo $line | awk '{print $5 }'`
                mnt_on=`echo $line | awk '{print $6 }'`
                mysql --user=your_id --password=your_password yourdb << EOF
                        echo "$server|$d_date|$m_date|$y_date|$vol|$blocks|$used|$avail|$cap|$mnt_on"
                        insert into your_table (SERVER_MACHINE,DATE_ID,MONTH_ID,YEAR_ID,Filesystem,Size_GB,Used_GD,Available_GB,USE_PCNT,Mounted_on) \
                        values ($server,$d_date,$m_date,$y_date,$vol,$blocks,$used,$avail,$cap,$mnt_on);
                        EOF
        }
        fi

done < /tmp/dfout
0
 
LVL 8

Accepted Solution

by:
point_pleasant earned 500 total points
ID: 35150477
oops left a debug in and forgot to toss the % sigh in space used

df -P >/tmp/dfout
server=`hostname`
d_date=`date +"%Y%m%d"`
m_date=`date +"%Y%m"`
y_date=`date +"%Y"`
toss_header=0
while read line
do
        if [ "$toss_header" -eq 0 ]
        then {
                toss_header=1
        } else {
                vol=`echo $line | awk '{print $1 }'`
                blocks=`echo $line | awk '{print $2 }'`
                used=`echo $line | awk '{print $3 }'`
                avail=`echo $line | awk '{print $4 }'`
                cap=`echo $line | awk '{print $5 }' | tr "%" " "`
                mnt_on=`echo $line | awk '{print $6 }'`
                mysql --user=your_id --password=your_password yourdb << EOF
                         insert into your_table (SERVER_MACHINE,DATE_ID,MONTH_ID,YEAR_ID,Filesystem,Size_GB,Used_GD,Available_GB,USE_PCNT,Mounted_on) values ($server,$d_date,$m_date,$y_date,$vol,$blocks,$used,$avail,$cap,$mnt_on);
                        EOF
        }
        fi

done < /tmp/dfout
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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