Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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".
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%.
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:
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.
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...?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.