4

I would like to know the server load divided by the amount of CPU cores in order to judge which servers are overloaded. I can get the amount of CPU cores with:

$ grep -c processor /proc/cpuinfo
2

I can get the server load with:

$ uptime | awk '{print $11}'
0.47,

Now I would like to divide 0.47/2 to get 0.23. How would I know which manpage to read to do this? I'm really looking for how I can learn what the next step in finding the correct command is, I'm not asking for the command itself.

I've tried searching the question online and even Google couldn't steer me in the right direction. Thus, alternatively, which keywords should I be searching on? The obvious "linux, division, bash, output" did not help.

dotancohen
  • 15,864

5 Answers5

5

This should do the trick:

(awk '{printf "%s/", $2}' /proc/loadavg; grep -c processor /proc/cpuinfo;) | bc -l

Also, you should get the load from /proc/loadavg where the command uptime also gets it.

chaos
  • 48,171
2

Just do bash scripting. Get both values to 2 different variables. Then by using command bc divide one from other. As simple as that.

  • 1
    Thank you, this got me 90% of the way there. How can I get rid of the , character in the uptime output? – dotancohen Jun 10 '14 at 07:02
  • Could always change the way you're getting load avg and use this - awk -F '[ ,]' ' { print $16 } ' <- which will return only the number rather than the number and the , – Lawrence Jun 10 '14 at 09:06
  • use cut command after to chop off ",". Use "," as Field separator and print first field.Like uptime | awk '{print $11}' | cut -d"," -f1 – Tingrammer Jun 10 '14 at 09:51
  • Thank you Tingrammer. I did accept chaos' answer as it taught me the most reusable knowledge, but I do appreciate your hinting me in the right direction and the refrain from just throwing me a fish. To be honest, I did have to google for how to get each individual value into a variable, but at least you pushed me in the right direction, as that is what I asked for. Thank you! – dotancohen Jun 10 '14 at 14:26
1
decimal_places=2
dc  <<< "${decimal_places}k $(uptime | awk '{print $11}'|  cut -d"," -f1) $(grep -c processor /proc/cpuinfo) /p"

This answer uses $() in bash so no temporary variables needed. It also uses dc, a calculator. We tell dc the precision using k, then give it 2 numbers, then ask it to divide them and print the answer.

1

Ksh and zsh have floating point arithmetic. Other shells including bash have only integer arithmetic (limited to 32 bits or 64 bits).

Every shell's man page has a section on arithmetic. If you want floating point calculations, it's harder to find. The primary way to search for man pages is the apropos command, which searches the pages' one-line descriptions. Since you're after something to use from the shell, you want section 1. apropos -s 1 float turns up nothing relevant on my Linux machine. What ends up working is

$ apropos -s 1 calculator
bc (1)               - An arbitrary precision calculator language
dc (1)               - an arbitrary precision calculator

The web has a lot more resources to offer. On the first page of http://www.google.com/search?q=bash+float, I find How to do integer & float calculations, in bash or other languages/frameworks? and now your question (and also several Stack Overflow threads). http://www.google.com/search?q=bash+division gives me several SO threads, the second one being how can I get a float division in bash?. You can also search on Unix Stack Exchange which has a question tagged and now also : How to do integer & float calculations, in bash or other languages/frameworks?

A common choice for floating point or large integer calculations is bc. It's a classic Unix utility which has been standardized.

Another standard utility that can do floating point computation and is perhaps easier to get started with than bc's precision-based model is awk. While awk is primarily designed for text processing, it can also do floating-point calculations.

-1

If you are using zsh, then please try:

echo $(($(grep -c processor /proc/cpuinfo) / $(uptime | awk '{print $11}')))

Screenshot

Ahmad Ismail
  • 2,678
  • Please note that most shells, and in particular bash (see OP's tags), do not perform floating point arithmetic but are limited to integer division, so in most cases this will not work as expected. – AdminBee Jun 09 '22 at 07:39
  • @AdminBee I normally use zsh. I tried this command in bash and it says bash: 6 / 1.09: syntax error: invalid arithmetic operator (error token is ".09"). Is it a zsh only solution? – Ahmad Ismail Jun 09 '22 at 07:46
  • Yes, unfortunately your solution is limited to zsh and ksh93 (few shells are capable of native floating point arithmetic). Since the OP has tagged the question explicitly as bash, your answer would seem to not address the question. – AdminBee Jun 09 '22 at 07:52
  • Maybe some zsh user will find it helpful. I myself might someday bump into this. who knows? – Ahmad Ismail Jun 09 '22 at 07:56
  • 1
    Well, since you have placed a disclaimer on top, I think it should be ok. Moderators may think otherwise, of course. – AdminBee Jun 09 '22 at 08:06