LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-02-2016, 09:52 AM   #1
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Rep: Reputation: Disabled
DF and AWK Command


I want to run a command (make it a script) to show me which volumes that are 20% or higher in usage. I am having problem with following syntax as it is not showing me which volume is 20%+ usage.

Appreciate your help!

df | awk '$1 $5~/([2-9]|10)[0-9]/ {exit 1}' && echo 'No >=20s' || echo 'Over 20% Usage!'
 
Old 02-02-2016, 10:59 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Without seeing the results from df this is just a guess, but you might need to use "df -P" to ensure that long device names don't cause output to be split into two lines.
 
Old 02-02-2016, 11:55 AM   #3
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
resulting output is:
Over 20 Usage!

It does not list the volume that is 20% or higher & that is what I want listed in result/output.
 
Old 02-02-2016, 01:49 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
What is the output when you run "df" alone, and compare that to the output from "df -P".
 
Old 02-02-2016, 03:04 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
what is the purpose of the '{exit 1}'?
the way i see it, awk will always exit with 1 (=false), and therefore the combined A && B || C structure will always choose C. which results in output of "Over 20 usage".
 
Old 02-02-2016, 03:29 PM   #6
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
If it does, it exits code 1 and the || code executes (there is a drive over 20 used) and If it doesn't, it exits code 0 and the && code executes. I'm looking to list the volume which is 20 or more utilized.

In this example, I want DF output to show: /dev/sda1 80% and /dev/sda2 50%.

A typical DF output is

Filesystem 1K-blocks Used Available Use% Mounted_On
/tmpfs 370692 0 370692 0% /dev
/dev/sda1 19601408 15681126 3920281 80% /
/dev/sda2 1000 500 500 50% /boot

Last edited by ntbluez; 02-02-2016 at 03:31 PM.
 
Old 02-02-2016, 06:08 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
OK, so "-P" isn't the issue at all, though you really should use it so that output like this doesn't cause confusion:
Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_omega3g-rootvol
                      11963960   5975292   5357884  53% /
tmpfs                  8194160       204   8193956   1% /dev/shm
The line break, which would not occur with "-P", makes that harder to parse.

Exactly what in your original code do you think is going to print the volume name?
Quote:
Code:
df | awk '$1 $5~/([2-9]|10)[0-9]/ {exit 1}' && echo 'No >=20s' || echo 'Over 20% Usage!'
The awk command doesn't print anything at all, and the echo commands just print the text you have there. What do you want to see if there is more than one volume with >20% usage, or are you perhaps not concerned about that? Right now, the awk script exits as soon as it sees the first one, so it would have to be changed to make it see more than one. Are you looking for output all on one line, e.g.:
Code:
volume1 [volume2 ...] Over 20% usage!
or would one or more lines with just volume names suffice? Easiest is a separate line with just the first oversized volume name:
Code:
df | awk '$1 $5~/([2-9]|10)[0-9]/ {print $1; exit 1}' && echo 'No >=20s' || echo 'Over 20% Usage!'
 
Old 02-02-2016, 06:20 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
exit stops the run - therefore no print of the record. And even if it did, you'd only ever get one record, not all over 20.

There is rarely any need to shell out of awk to process its output by other tools. awk is more than capable of handling all you require in this case. As for that regex, that is only needed in cases where the tool doesn't have arithmetic comparison operators. awk does have those operators, so use that instead.
 
Old 02-02-2016, 09:20 PM   #9
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
You Are Awesome!!!

One more, what if I wanted to also include the hostname? Meaning, I am using a listing of server names to run this script against, and I was wondering how to include that hostname while using this DF command in a SSH session...?

ssh MyUserID@ServerName df | awk '$1 $5~/([2-9]|10)[0-9]/ {print $1; exit 1}' && echo 'No >=20s' || echo 'Over 20 Usage!' && hostname


I was thinking something like adding additional command like "&& hostname" but having no luck as it produces my local host name.

Thanks again!!!

Last edited by ntbluez; 02-02-2016 at 09:25 PM.
 
Old 02-03-2016, 02:05 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by ntbluez View Post
I was thinking something like adding additional command like "&& hostname" but having no luck as it produces my local host name.
so what hostname do you want then? not sure i understand.

PS:
if you're actually using this as a script, meaning it is saved as an executable file, i would rather not use the oneliner-style with && and ||, but write it properly multiline instead.
execution time will be the same (if you do it right of course), but you have much more control and options.
 
Old 02-03-2016, 06:52 AM   #11
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
@Ondoho

Yes. I want to run these commands via script. If you suggest I do so, what would the script look like? I know I have a file called SERVERS which lists those servers that ssh uses to make each connection. I want the "Hostname" of that server to be included in output of that DF command...

So, lets say I have a file called SERVERS with three (3) server names; server1.com server2.com and server3.com.
Requirement: need SSH make connection to each server and perform these commands to collect information about that server (a) list any disk volume greater than or equal to 70 utilization (b) list % of usage for that volume 70%+ (c) provide that server hostname. I want results to be in an output file on my local PC or server which I run script from.

Space.sh...
#!/bin/sh
for SERVERS in 'cat server-list'
do
ssh MyID@$SERVERS df | awk '$1 $5~/([2-9]|10)[0-9]/ {print $1; exit 1}' && echo 'No >=20s' || echo 'Over 20% Usage!' && grep $HOSTNAME=* >> /home/joe/script/something.txt
done
 
Old 02-03-2016, 08:40 AM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Just pass the server name to awk as a variable and include that variable in the print statement:
Code:
awk -v "Server=$SERVER" '$1 $5~/([2-9]|10)[0-9]/ {print Server, $1; exit 1}'
 
Old 02-03-2016, 03:36 PM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i think there's a problem with your script; at least on my system it spits out only the first partition that is over 20% usage; although most of my partitions are.

anyhow, i felt like transforming this into a proper script:
Code:
#!/bin/bash

while read line
do
perc="${line%%\%*}"
if (( perc > 19 ))
then
echo $line
fi
done <<<"$(df --output=pcent,source)"
to understand, read
Code:
man df
and this:
Quote:
Originally Posted by http://www.tldp.org/LDP/abs/html/string-manipulation.html

${string%%substring}

Deletes longest match of $substring from back of $string.

stringZ=abcABC123ABCabc
echo ${stringZ%%A*}
$ abc
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
turning awk command line entries into awk scripts tabbyagirl Linux - Newbie 5 08-22-2013 12:46 AM
About awk command incomingid Linux - Newbie 5 04-01-2009 10:44 PM
awk command toaravind Linux - General 5 07-02-2008 01:25 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration