Bug 214297 - net-mgmt/nagios-check_cpu_usage: Sometimes fails to parse vmstat output
Summary: net-mgmt/nagios-check_cpu_usage: Sometimes fails to parse vmstat output
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-ports-bugs (Nobody)
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2016-11-07 18:59 UTC by Vidar Karlsen
Modified: 2017-08-22 16:43 UTC (History)
0 users

See Also:


Attachments
Change vmstat to iostat (468 bytes, patch)
2016-11-07 18:59 UTC, Vidar Karlsen
no flags Details | Diff
Change vmstat to iostat (svn diff) (1006 bytes, patch)
2016-11-10 20:09 UTC, Vidar Karlsen
vidar: maintainer-approval+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vidar Karlsen 2016-11-07 18:59:48 UTC
Created attachment 176748 [details]
Change vmstat to iostat

nagios-check_cpu_usage reads output from vmstat in order to report CPU usage.
vmstat will output straight numbers (not "easily human readable form") when the output is not a terminal.
On a busy host, this may cause the fre and flt columns to be jammed together with no separating whitespace, which in turn will cause nagios-check_cpu_usage to return bogus values due to a sort of off-by-one error:

Not busy:
r b w     avm     fre  flt  re  pi  po    fr   sr   in    sy    cs us sy id
0 0 3  762140  490772  693   0   0   0   863    4   70   833   366  1  0 99
               ^^^^^^  ^^^
Busy (just testing running make in security/openssl):
r b w     avm     fre  flt  re  pi  po    fr   sr   in    sy    cs us sy id
1 0 3  865856  48101239575   0 121   0 43154    8   72 40026  1357 21  5 74
               ^^^^^^^^^^^
nagios-check_cpu_usage reads the 15th, 16th and 17th column, so in the latter case it will report us="5", sy="74", id="".

I propose changing the following line:
cpu_all=$( vmstat -c 2 -n 0 | tail -n 1 | awk '{print $15 " " $16 " " $17}' )
to:
cpu_all=$( iostat -c 2 -t proc | tail -n 1 | awk '{print $3 " " $5 " " $7}' )
or to:
cpu_all=$( vmstat -h -c 2 -n 0 | tail -n 1 | awk '{print $15 " " $16 " " $17}' )

iostat doesn't appear to have this problem because it outputs much fewer columns, while the second alternative solution bypasses the problem by forcing vmstat to print the memory values as 1M instead of 1048576.
Comment 1 Vidar Karlsen 2016-11-10 20:09:34 UTC
Created attachment 176870 [details]
Change vmstat to iostat (svn diff)

Implicit approval, the port has no maintainer
Comment 2 commit-hook freebsd_committer freebsd_triage 2017-08-22 16:42:19 UTC
A commit references this bug:

Author: swills
Date: Tue Aug 22 16:41:18 UTC 2017
New revision: 448562
URL: https://svnweb.freebsd.org/changeset/ports/448562

Log:
  net-mgmt/nagios-check_cpu_usage: switch to iostat

  PR:		214297
  Submitted by:	Vidar Karlsen <vidar@karlsen.tech>

Changes:
  head/net-mgmt/nagios-check_cpu_usage/Makefile
  head/net-mgmt/nagios-check_cpu_usage/files/check_cpu_usage
Comment 3 Steve Wills freebsd_committer freebsd_triage 2017-08-22 16:43:19 UTC
Committed, thanks!