Task:
stdout the load avg from top in a decimal form like (e.g 0.23)
Details:
I need to parse this script to Chef's inspec and check if its result is bigger/smaller than something, for example:
describe command("top -b -n 1 | awk '/load average/ { sub(/,/,\".\",$10); printf \"%f\n\",$10}'") do
its("stdout") { should eq 0.00 }
end
This example returns ""
But now when I think about it, I could compare with a file in /proc/loadavg
Progress
Used this resource: Grab the load average with top
With this command, I get a good representation of the output, but it's a string and I can't do any mathematical operations with it:
martin@martinv-pc:~$ top -b -n 1 | awk '/load average/ { printf "%s\n", $10}'
0,63,
But when I try to change the printf to decimal/float, I get an error:
martin@martinv-pc:~$ top -b -n 1 | awk '/load average/ { printf "%f\n", $10}'
0.000000
martin@martinv-pc:~$ top -b -n 1 | awk '/load average/ { printf "%d\n", $10}'
0
Can't echo, tried with cut, bad idea -- not working:
martin@martinv-pc:~$ top -b -n 1 | awk '/load average/ { printf "%s\n", $10}'|cut -c1-4
0,15
martin@martinv-pc:~$ top -b -n 1 | awk '/load average/ { printf "%s\n", $10}'|$((cut -c1-4))
-4: command not found
Another attempt:
martin@martinv-pc:~$ top -b -n 1 | awk '/load average/ BEGIN { printf "%.f\n", $10};'
awk: line 1: syntax error at or near BEGIN
Question:
How can I convert the string value to decimal/float/integer ?
ps -o user,rss
output:
[vagrant@localhost ~]$ ps -o user,rss
USER RSS
vagrant 736
vagrant 1080
Task:
sdout the load avg from top in a decimal form like(e.g 0.23)
Solution:
top -b -n 1 | perl -lane 'print "$1.$2" if /load average: (\d+)[,.](\d+)/'
Notes:
This retrieves the 1m load average. It looks like this is what you want, but you should state it clearly. If needed, the code can be easily modified to retrieve the load averages over 5 minutes or 15 minutes, or even all three.
As pointed out by @terdon, uptime
might be a better starting point than top
in this case.
After the first two lines, you obscurely describe what you want to do with the result. Subsequent steps you want to take should be the subject of new questions.
In Perl, numbers are auto-casted to strings and vice-versa. Any numerical operation can be performed on a string representing a number. e.g. print "$1.$2"+11.11
Question 2:
This part is about the second question, which is totally unrelated to the first one.
I urge the OP to post this question separately.
How Can I convert the string value to decimal/float/integer ?
Better written as: Performing numeric comparisons on strings with Chef's InSpec.
Solution:
Convert the string to a numeric format, with either to_i
or to_f
.
Example:
describe command("echo 1.00") do
its("stdout.to_f") { should be < 1.03 }
end
Explanation:
Very reasonably, stdout is treated as a string. Also very reasonably, numeric comparisons require the two numbers to be...numbers. Luckily, conversion can be done with the handy Ruby string methods: to_i
, to_f
, to_r
and to_c
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments