Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
    
Accepting this answer because it taught me the most reusable knowledge. Thank you. –  dotancohen Jun 10 at 14:24
add comment

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

share|improve this answer
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 at 7: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 at 9: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 at 9: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 at 14:26
add comment
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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.