Solved

Shell Script to insert into a database

Posted on 2011-03-15
2
549 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

733 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