0

I'm writing a script which utilises the salt-cli (SaltStack) as well as generic command line arguments to produce a simple HTML-based table outlining all of our servers' Hardware specs and software versions.

It's as simple as it sounds, however my only challenge is stripping my commands outputs so that they can be presented nicely in a table (e.g. one word/number rather than the whole output).

My latest challenge that I'm yet to strip effectively is the output of a simple 'df -Ph. So far I have got it down to this:

'df -Ph /'

My output:

 Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vhfhuffu               50G  8.0G   39G  18% /

I would like it to only show the available for each server, I can't find a reliable way to do this for ever Unix server.

jto
  • 175
  • 6
  • 21
  • What's exactly the question? Just extract `39G` in above example of sum up possible different filesystems? And how is this related to grep? – Serge Ballesta Aug 03 '17 at 07:39
  • Because I need to find a way of finding the sum of 'Avail' for every machine, I am running this command across 15 servers. @SergeBallesta – jto Aug 03 '17 at 07:49

4 Answers4

0

tail and cut should be enough here:

 df -Ph / | tail +2 | cut -f4 -w

should give you the file system size of the root file system.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Another simple way is to combine tail -n 1 to get the last line and pass it through grep '{print $4}'. This will extract the39G` value that it sounds like you want, e.g.

$ df -Ph / | tail -n 1 | awk '{print $4}'
39G

If it is some other number you are trying to sum, just let me know.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I ended up using something similar to this method: df -Ph / | grep / | awk '{ print $4}', I'm just struggling to physically run this command now as I have to run it via the salt-cli i.e, salt '*' cmd.run 'df -Ph / | grep / | awk '{ print $4}'', it doesn't like the fact the print command ends in ' as well as the salt-cli syntax, how can I get around this? @David C. Rankin – jto Aug 03 '17 at 08:37
  • I was curious. This is the first I've ever hear of a `salt-cli`. Is it some new shell I haven't heard of, or is it more like a PowerShell windows thing? – David C. Rankin Aug 03 '17 at 08:42
  • It is a remote execution and configuration management tool which has a command line feature much like aws-cli – jto Aug 03 '17 at 08:44
  • The syntax is as follows: salt '' cmd.run '' my problem is that my command uses ' ' along with the salt-cli syntax, so it doesn't like it and throws as error – jto Aug 03 '17 at 08:51
  • Have you tried escaping the quote (but that may give `awk` heartburn. I'd google a bit and see if there is anything salt specific related to quoting that pops up. – David C. Rankin Aug 03 '17 at 08:54
  • I've tried but to no avail, there isn't much out there about Salt minus their own documentation – jto Aug 03 '17 at 09:01
  • I'll try digging -- I'm interested in what salt is. But that will be in the morning... I'm squinting at the keyboard as it is now `:p` – David C. Rankin Aug 03 '17 at 09:33
  • I found a solution, you led my down the right route with the escaping comment! I had to do it the ugly way (e.g. '"'"'{ print $4}'"'"'') – jto Aug 03 '17 at 10:15
0

You are able to extract the needed column value using --output option:

df -h --output=avail | tail -n +2
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

I ended up going with this due to my need to run this command from a script via Salt which requires '' around the cmd.run

df -Ph / | grep / | awk '"'"'{ print $4}'"'"''

Ugly but it's the only thing that works in such a niche case

jto
  • 175
  • 6
  • 21