I am working on a program which will send an snmp alert when /var reaches maximum threshold. I am having trouble with calculating % of /var disk usage for this.
I have this command “du -hs /var” which give me usage of /var in MB and /var is in the /root directory. So to calculate total disk on which /var is present I did the following command df -ks, this gives me the total of / and some % which I am not sure I should use. Can please someone help with a command for calculating the %.
Advertisement
Answer
Does this work for you?
df /var | tail -1 | sed -r 's/.* ([0-9]+%).*/1/'
Breaking this down:
df /var
outputs the header and one line for /var
.
tail -1
strips the header.
The sed
command edits the line, capturing the percentage used and
discarding the rest. In words, we could say “match everything up to a
space, followed by one or more digits and a percent sign; capture the
digits and percent sign; match the rest of the line; replace the line
with the part that was captured”.
This produces a nice human-readable number with a percent sign. For computing purposes you probably want the number without the percent sign:
df /var | tail -1 | sed -r 's/.* ([0-9]+)%.*/1/'