Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Shell Script to insert into a database

Posted on 2011-03-15
2
Medium Priority
?
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

647 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