How to get df output into MySQL using bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get df output into MySQL using bash?
# 1  
Old 07-08-2010
How to get df output into MySQL using bash?

Hi All,

I want to run some commands and get the output into a mysql database.

I want to run the df cmd into a text file, then import it into my db sysinfo using a bash script.
My table "df" has the following 9 fields:

server, filesystem, size, used, available, useperc, mounted, date, time

cmd for df creates it in a comma separated output.
Code:
df -k | tr -s " " | sed 's/ /, /g' | grep -v Filesystem > df.txt

bash script for loading data:
Code:
#!/bin/bash
/usr/bin/mysql --user=<user> --password=<password> --database=sysinfo<<EOFMYSQL

load data local infile 'df.txt'  INTO TABLE df fields terminated by '\,' LINES
TERMINATED BY '\n'; 
EOFMYSQL

Unfortunately this is not working and I only get the first line imported.

Can some one show me how to fix this script, I think it may be to do with the line terminators but can't find a solution.

I would also like to prepend the host name for field 1 and the date and time in the last 2 fields.

I'm very new to sql would really apreciate some help and advice.
# 2  
Old 07-08-2010
You could try something like this:

- assuming a table structure like this:

Code:
create table df (
  hostname varchar(50),
  filesystem varchar(100),
  blocks int,
  used int,
  available int,
  capacity varchar(4),
  mounted varchar(100),
  ts timestamp default current_timestamp 
  );

- assuming paths with no white space characters:

Code:
df -Pk | awk 'NR > 1 { 
  $1 = $1; printf "insert into df values ( \47%s\47,", h 
  for (i = 0; ++i <=NF;)
    printf "\47%s\47,", $i
  print " current_timestamp );"     
  }' OFS=, h="$(hostname)" | 
  mysql -u <username> -p<password> <db_name>

These 2 Users Gave Thanks to radoulov For This Post:
# 3  
Old 07-09-2010
Thanks Radoulov,

Worked perfectly, now I just need to work out how it works Smilie
# 4  
Old 07-09-2010
The awk script just parses the df output and generates insert statements that are send to mysql client for execution.
If you tell me which part is not clear, I'll try to explain it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mysql output to file

Hi, Below is my code to execute mysql statement , how to save the output to a file ? #!/usr/bin/bash #Script to run automated sql queries #Declaring mysql DB connection MASTER_DB_USER='root' MASTER_DB_PASSWD='root' MASTER_DB_PORT=3306 MASTER_DB_HOST='localhost'... (1 Reply)
Discussion started by: Pratik4891
1 Replies

2. Shell Programming and Scripting

Conditional bash/mysql query help

I think(hope) I've got a simple one - I just need to send an email if a mysql query returns any results (ideally - it will never match). Currently I just pipe the mysql query output to the mail program, but of course that emails regardless of the output( and I check this every 10 minutes from... (5 Replies)
Discussion started by: jcass78
5 Replies

3. Shell Programming and Scripting

Technical questions on bash,mysql and pHp

1. bash -bash escape rules, esp. ',", -how to use Ctrl+R reverse cmd search with regex? 2. mysql -how to use grep in mysql 3. php -why !0 is not evaluated to true? what's its value -php getopt: what if there is duplicate in cmdline args (2 Replies)
Discussion started by: John_Peter
2 Replies

4. UNIX and Linux Applications

Mysql erratic output

Hi,this doesnt give any output mysql -uroot -phruti2 -B -e "use WebNmsDB;select HEADERINDEX from meas_headers where CLLI= '$clli' and IVALSTART>'$ivalstart' and IVALSTART<'$ivalend' and RPTTYPE='$rpttype';" > asdf.txt This generates "good" output mysql -uroot -phruti2 -B -e "use... (1 Reply)
Discussion started by: leghorn
1 Replies

5. Shell Programming and Scripting

[BASH] Gawk + MYSQL script

Hello! I've got script to write. It should read databases (names, volumes) from table testdatabase and compares it to actually existing databases in /var/lib/mysql/. If there is no informations about database in table - we should see information "There is no declared informations about database... (1 Reply)
Discussion started by: Zimny
1 Replies

6. Programming

Loop in bash file for mysql

Hi, I'm having problems with a script which some of you helped me with last week. It's a script to check the status of orders in an SQL database, and if the count(*) is zero, then the script stops. If it's non-zero, the script echos the result to a file and then in cron, I cat the file and mail... (3 Replies)
Discussion started by: davidm123SED
3 Replies

7. Shell Programming and Scripting

How to run bash script from within MySQL

Can someone tell me the syntax to run a shell script from within MySQL? I know there is a way to do it, but I can't remember the syntax. I thought it was something like: mysql> \. /user/myscript; but I get this error: Failed to open file '/user/myscript;', error: 2 Please help!... (4 Replies)
Discussion started by: peterv6
4 Replies

8. Shell Programming and Scripting

mysql and bash...

Ok the issue I have is a bit complicated, for me, and my understanding of shell programming and bash is non-existent. Background: I do web development for a team of programmers that make custom kernels for the Android OS. The guys are using a bash script (through gerrit or github, like I said... (4 Replies)
Discussion started by: Nyght
4 Replies

9. Shell Programming and Scripting

bash mysql export to file

I'm trying to export a mysql query to a .csv file, right now I'm running a successful query like: us_id=`mysql -u $USER_NAME --password=$PASSWORD -D "databasename" \ -e "SELECT * \ FROM databasename.table \ WHERE somefield >0 AND otherfield ='$ctry' \ ORDER BY... (1 Reply)
Discussion started by: unclecameron
1 Replies

10. Shell Programming and Scripting

Passing a MySql password from bash script

Hi all, I am running this script on Mandrakelinux release 10.1, 2.6.8.1-12mdksmp #1 SMP I have also installed 'expect' separately. I have created an Rsync script, but before any Rsync command does run, a MySql dump must be done first, and I am battling a bit to pass the MySql password from... (2 Replies)
Discussion started by: codenjanod
2 Replies
Login or Register to Ask a Question