Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active June 9, 2022 20:05
Show Gist options
  • Save mjf/eb4dc2d52dfc2cab1961 to your computer and use it in GitHub Desktop.
Save mjf/eb4dc2d52dfc2cab1961 to your computer and use it in GitHub Desktop.
Linux Load Average Percentage (suitable as HAProxy Agent Check)

Manual

ABOUT

   loadavgpct - Linux Load Average Percentage
   loadavgpct - Linux Load Average Percentage Capacity

SYNOPSIS

   loadavgpct[cap] [LOADAVG_FIELD_1] [LOADAVG_FIELD_2] [COEFFICIENT]

EXAMPLE

   $ loadavgpctcap 1 2 1
   $ loadavgpct - - 10
#! /bin/sh
# loadavgpct - Linux Load Average Percentage
# Copyright (C) 2015 Matous Jan Fialka, <http://mjf.cz/>
# Released under the terms of "The MIT License"
export LC_ALL=C
export LANG=C
awk -v A=$1 -v B=$2 -v C=$3 '
BEGIN {
A = ((A + 0) == 0) ? 1 : A
B = ((B + 0) == 0) ? 2 : B
C = ((C + 0) == 0) ? 0 : C
N = 0
}
(ARGIND == 1) && ($1 == "processor") {
N++
}
(ARGIND == 2) {
A = $A
B = $B
}
END {
printf "%d%%\n", (100*(A + B*C))/(A + C*(B + N) + N)
}
' /proc/cpuinfo /proc/loadavg
# vi:ft=sh
#! /bin/sh
# loadavgpctcap - Linux Load Average Percentage Capacity
# Copyright (C) 2015 Matous Jan Fialka, <http://mjf.cz/>
# Released under the terms of "The MIT License"
export LC_ALL=C
export LANG=C
awk -v A=$1 -v B=$2 -v C=$3 '
BEGIN {
A = ((A + 0) == 0) ? 1 : A
B = ((B + 0) == 0) ? 2 : B
C = ((C + 0) == 0) ? 0 : C
N = 0
}
(ARGIND == 1) && ($1 == "processor") {
N++
}
(ARGIND == 2) {
A = $A
B = $B
}
END {
printf "%d%%\n", (100*(C + 1)*N)/(A + C*(B + N) + N)
}
' /proc/cpuinfo /proc/loadavg
# vi:ft=sh
service loadavgpct
{
disable = no
type = UNLISTED
protocol = tcp
port = 5623
user = root
wait = no
# server = /usr/local/sbin/loadavgpct
server = /usr/local/sbin/loadavgpctcap
log_on_success =
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment