hi guys,

Im in the process of writing a unix mointoring program that will monitor differenet servers all runnning solaris . I am creating a main screen with summary warnings for high cpu usage, low disk space, meta device faults, high network collisions etc. All has gone well but now im coming to do the memory summary field.

Bacially I want to display a warning if memory usage exceeds 80%. Now i know sar -r shows free memory and prtdiag -v | grep Mem will show total memory of the system. But these figures dont seem to relate to eachother. For example a system has 8192mb of memory installed and the sar -r output shows 439484 being free. But how do these figures relate to each other as a percentage so is that 50% of memory being used.

It would be really helpful if someone could write me a little script to collect memory statistics for 30 seconds and display it as a percentage when related to total memory.

Can anyone help!!!

hi thought i would include the code i have already written.

The first file is run from crontab every ten minutes and simply collects data or sar -r and just selects the second column the memory usage:

#memory usage
#
sar -r 1 30 | awk ' { print $2 } ' | grep [0-9] > $MEM_BASE.$TODAY.$NOW_HM

This is then collected by the monitoring program and i have started to manipulate it.

#  #mem checks
#  #
  if [[ ! -f "$IGNORE_WARN ]] || ! grep "MEM" $IGNORE_WARN >/dev/null 2>&1
  then
    MEM_H=0
    if [[ -n $MEM_F ]]
    then
      cat $MEM_F | grep [0-9] | while read MEM_LINE
      do
        MEM=$((` print $MEM_LINE | nawk '{ print ($1) }'`))
        if (( MEM <=  ????? ))
        then
          MEM_H=1
        fi
      done
    fi
    if (( MEM_H == 1))
    then
      print -n " ${_TP_BLINK}MEM${_TP_RMSO} "
      if ! grep MEM /var/monitoring/servers/$CURRHOST/mondata/$SNAME >/dev/null 2>&1
        then
          mailx -s "MEMORY USUAGE HIGH ON $CURRHOST" $MAILADDR >/dev/null 2>&1
      fi
    fi
  fi

basically it goes through checks that we dont want to ignore the memory warning then if we dont check that the file collected from the first script is present read each of the 30 lines and check them against ..... this is where im stuck i need to know whether the sar -r output actually shows real memory usage (not paged or swap etc) and then if the server has 8192mb of memory how to get the two figures into the same form e.g mb or kb and compare them.

Hey There,

For sar -r, the freemem is the number of pages available the freeswap is number of disk blocks available for swapping (although the blocksize can vary depending on your setup)

Basically, yes, you should be able to convert your freemem from sar -r by multiplication against the size of your system's memory pages, which you should be able to get with the "pagesize" command which will return its output in bytes:

8192 = 8k <-- Not that I don't think you know that; just trying to be complete ;)

Hope that helps :)

, Mike

hi thanks for the great reply and you have answered my question but i now have a kinda new one based on the old one!!!

basically if i do sar -r on a system with 2gb memory i get that there is 160mb free whereas if i do wmstat i get that there is 1.5gb of memory free. Im trying to work out which figure is correct and most accuratly displays the memory usage on a sun box.

chris

Hey There,

Glad that helped you out. Sometimes comparing the two can be difficult since they output in different increments. I believe vmstat shows free memory in Kb.

If you could, can you post the output from sar -r for whatever time that you run it and then also run vmstat 1 2 (so that I can check out the line after the summary line it always spits out).

Basically, where Solaris is concerned, I usually assume the higher of two numbers is correct (although in this case the divide seems to large to be accurate, although it could be). Solaris' internal memory management hides free memory from itself, top, and other commands. What I mean is, if Solaris allocates memory for use, it will show it as "used" until a program attempts to claim it, at which point it frees it up - very confusing, but that's how they do it ;)

I'll look for your output and hope I can piece that together for you :)

, Mike

will do on monday cheers

Cool, I'll look for it then.

Private message me the info if you don't feel comfortable posting it. No problem :)

, Mike

ok output from sar -r 1 30


10:07:46 freemem freeswap
10:07:47 165228 7189814
10:07:48 165228 7189814
10:07:49 165228 7189814
10:07:50 165412 7198052
10:07:51 165193 7189275
10:07:52 165193 7189275
10:07:53 165193 7189275
10:07:54 165409 7191382
10:07:55 164936 7188990
10:07:56 164448 7189307
10:07:57 164448 7189307
10:07:58 164580 7189180
10:07:59 164627 7195057
10:08:00 164499 7188990
10:08:01 164466 7189307
10:08:02 164460 7189038
10:08:03 162994 7112126
10:08:04 166182 7260432
10:08:05 164467 7187390
10:08:06 164472 7189307
10:08:07 164628 7193315
10:08:08 164659 7186741
10:08:09 166288 7259824
10:08:10 162868 7117020
10:08:11 164451 7186947
10:08:12 164485 7189323
10:08:13 164552 7191224
10:08:14 164654 7194345
10:08:15 164480 7189291
10:08:16 164600 7191129

Average 164742 7189714


vmstat 1 30 (thought i wld run them for the same ammount of time)

kthr memory page disk faults cpu
r b w swap free re mf pi po fr de sr m0 m1 m5 m6 in sy cs us sy id
0 0 0 3924952 1514712 88 921 2 8 8 0 0 0 0 2 0 316 1315 233 4 6 91
0 0 0 3630936 1329632 177 2229 47 0 0 0 0 0 0 6 0 319 2500 327 4 11 85
1 0 0 3630928 1329672 455 4957 8 8 8 0 0 0 0 15 0 668 6564 724 19 23 58
4 0 0 3633160 1330536 351 4384 8 0 0 0 0 0 0 8 0 385 4869 448 11 19 70
0 0 0 3630936 1329552 329 4179 0 0 0 0 0 0 0 4 0 315 4507 417 7 17 76
3 0 0 3630072 1329760 326 4148 0 0 0 0 0 0 0 6 0 416 4567 430 14 18 68
0 0 0 3631504 1330096 535 5832 16 16 16 0 0 0 0 23 0 772 7949 935 18 28 54
0 0 0 3630936 1329424 326 4140 0 0 0 0 0 0 0 14 0 397 4538 432 12 18 70
0 0 0 3630912 1329400 427 4418 16 0 0 0 0 0 0 10 0 701 6202 702 14 22 64
0 0 0 3630936 1329296 238 3040 0 0 0 0 0 0 0 3 0 333 3428 382 9 13 78
0 0 0 3630928 1329288 323 4107 0 0 0 0 0 0 0 4 0 419 4531 434 14 18 69
3 0 0 3634016 1330320 512 5608 16 0 0 0 0 0 0 17 0 750 7706 865 18 27 55
0 0 0 3630936 1329176 303 3712 0 0 0 0 0 0 0 25 0 355 4027 383 11 16 73
0 0 0 3630928 1329168 448 4866 16 16 16 0 0 0 0 12 0 713 6546 725 18 23 59
0 0 0 3630936 1329072 493 5578 0 40 40 0 0 0 0 12 0 607 7226 730 17 55 28
4 0 0 3631392 1329760 369 4413 16 0 0 0 0 0 0 12 0 462 5159 472 15 20 66
0 0 0 3630936 1328976 311 3849 0 0 0 0 0 0 0 3 0 309 4176 378 6 17 77
0 0 0 3630936 1328976 245 3029 0 39 39 0 0 0 0 24 0 538 3805 456 13 15 73
kthr memory page disk faults cpu
r b w swap free re mf pi po fr de sr m0 m1 m5 m6 in sy cs us sy id
2 0 0 3632992 1330304 830 7377 16 0 0 0 0 0 0 41 0 587 10492 901 20 35 45
3 0 0 3632128 1329384 323 4096 0 0 0 0 0 0 0 3 0 300 4431 401 8 18 75
0 0 0 3630936 1328928 326 4138 0 0 0 0 0 11 0 2 0 496 4483 480 7 19 74
0 0 0 3630936 1328928 464 5395 0 79 79 0 0 0 0 11 0 520 6700 685 16 25 59
0 0 0 3631008 1329016 401 4633 16 0 0 0 0 0 0 63 0 638 5548 566 20 20 60
0 0 0 3630912 1328896 542 5839 16 79 79 0 0 0 0 15 0 758 7432 703 20 27 53
0 0 0 3630936 1328912 215 2640 0 0 0 0 0 0 0 0 0 380 2947 343 10 12 78
0 0 0 3630928 1328904 176 2345 0 0 0 0 0 0 0 0 0 347 2608 322 3 10 87
6 0 0 3633640 1330192 326 4138 0 0 0 0 0 0 0 0 0 321 4476 403 7 18 75
0 0 0 3630928 1328904 323 4096 0 0 0 0 0 0 0 0 0 305 4503 407 8 18 75
0 0 0 3630928 1328904 319 3961 0 0 0 0 0 0 0 0 0 326 4388 400 9 17 74
0 0 0 3630912 1328888 267 3473 0 0 0 0 0 0 0 0 0 331 3843 372 6 15 79

(also tried changing the font to courier so all the columns lined up but coudlnt work out how!!)

Hey There,

You're actually pretty close on numbers. For instance, your sar -r shows:

10:07:46 freemem freeswap
10:07:47 165228 7189814
10:07:48 165228 7189814

with freemem being 165228 x pagesize (on my system 8192b/8k) :

# let a=8*165228
# echo $a
1321824

That's in k - so, approx 1.3gb free

and your vmstat otuput is in k, with general outputs of:

1329632

in k, by default, approx 1.3gb free.

I think you might have just gotten messed up by running vmstat 1 1 or something. The first lihe of that output is always a summary and it does show about 1.5gb free

Hope that helps. I think you're good both way :)

Let me know if I can help you out any further :)

, Mike

commented: cheers great advice +2

that is more then helpful thanks very much for the help

Awesome,

Glad I could help you out :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.