1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

Plugin-Gallery: Get better 2nd level headings

Review of category "system"
This commit is contained in:
dipohl 2017-02-24 23:54:53 +01:00
parent 54a91c13a4
commit 7fdb4741fe
27 changed files with 18 additions and 12 deletions

View file

@ -0,0 +1,84 @@
#!/bin/sh
#
# Plugin to monitor CPU usage, for a selected set of processes. Tested on FreeBSD.
#
# Author: Erik Cederstrand
# Based on http://waste.mandragor.org/munin_tutorial/cpubyuser
# Thanks to Yann Hamon.
#
# Usage: Place in /usr/local/etc/munin/plugins/ (or link it there using ln -s)
# Add this to your /ur/local/etc/munin/plugin-conf.d/plugins.conf:
# [cpubyproc]
# env.procs httpd java
#
# httpd and java being a list of the processes to monitor.
#
# Parameters understood:
#
# config (required)
# autoconf (optional - used by munin-config)
#
#%# family=auto
#%# capabilities=autoconf
if [ "$1" = "autoconf" ] ; then
if [ -n "$procs" ] ; then
echo "yes"
else
echo "\$procs not defined."
fi
exit
fi
if [ "$1" = "config" ] ; then
echo "graph_args --base 1000 -r --lower-limit 0";
echo "graph_title CPU usage, by process";
echo "graph_category processes";
echo "graph_info This graph shows CPU usage, for monitored processes.";
echo 'graph_vlabel %'
echo 'graph_scale no'
echo 'graph_period second'
echo "graph_order $procs"
FIRSTPROC=1;
for proc in $procs; do
echo "${proc}.label $proc"
echo "${proc}.info CPU used by process $proc"
echo "${proc}.type GAUGE"
if [ $FIRSTPROC -eq 1 ] ; then
echo "${proc}.draw AREA"
export FIRSTPROC=0;
else
echo "${proc}.draw STACK"
fi
done ;
exit
fi
for proc in $procs ; do {
ps axo 'pcpu,comm' | grep "$proc" |
awk '
BEGIN {
FS=" "
CPU_PROC=0
}
{
CPU_PROC+=$0
}
END {
print "'$proc'.value "CPU_PROC
}'
}
done;

118
plugins/cpu/cpu_ Executable file
View file

@ -0,0 +1,118 @@
#! /bin/sh
# Multigraph CPU plugin
# It shows a global cpu graph, and you can drill down per-core
PLUGINBASE=$(basename $0)
emit_config()
{
cat <<EOF
graph_title CPU usage $1
graph_order system user nice idle iowait irq softirq
graph_vlabel %
graph_scale no
graph_info This graph shows how CPU time is spent.
graph_category system
graph_period second
system.label system
system.draw AREA
system.min 0
system.type DERIVE
system.info CPU time spent by the kernel in system activities
user.label user
user.draw STACK
user.min 0
user.type DERIVE
user.info CPU time spent by normal programs and daemons
nice.label nice
nice.draw STACK
nice.min 0
nice.type DERIVE
nice.info CPU time spent by nice(1)d programs
idle.label idle
idle.draw STACK
idle.min 0
idle.type DERIVE
idle.info Idle CPU time
iowait.label iowait
iowait.draw STACK
iowait.min 0
iowait.type DERIVE
iowait.info CPU time spent waiting for I/O operations to finish when there is nothing else to do.
irq.label irq
irq.draw STACK
irq.min 0
irq.type DERIVE
irq.info CPU time spent handling interrupts
softirq.label softirq
softirq.draw STACK
softirq.min 0
softirq.type DERIVE
softirq.info CPU time spent handling "batched" interrupts
steal.label steal
steal.draw STACK
steal.min 0
steal.type DERIVE
steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running
guest.label guest
guest.draw STACK
guest.min 0
guest.type DERIVE
guest.info The time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.
guest_nice.label guest
guest_nice.draw STACK
guest_nice.min 0
guest_nice.type DERIVE
guest_nice.info The time spent running a virtual CPU for niced guest operating systems under the control of the Linux kernel.
EOF
}
CPUS=$(grep '^cpu[0-9]' /proc/stat | cut -d ' ' -f 1)
if [ "$1" = "config" ]
then
echo multigraph $PLUGINBASE
emit_config
# Emit one subgraph per core
for cpu in $CPUS
do
echo multigraph $PLUGINBASE.$cpu
emit_config $cpu
done
exit 0
fi
emit_values()
{
while read key user nice system idle iowait irq softirq steal guest guest_nice
do
[ "$guest_nice" = "" ] && guest_nice=0
[ "$key" != "$1" ] && continue
cat <<EOF
system.value $system
user.value $user
nice.value $nice
idle.value $idle
iowait.value $iowait
irq.value $irq
softirq.value $softirq
steal.value $steal
guest.value $guest
guest_nice.value $guest_nice
EOF
done < /proc/stat
}
# Values
echo multigraph $PLUGINBASE
emit_values cpu
# Emit one subgraph per core
for cpu in $CPUS
do
echo multigraph $PLUGINBASE.$cpu
emit_values $cpu
done
exit 0

111
plugins/cpu/cpu_by_process Executable file
View file

@ -0,0 +1,111 @@
#!/usr/bin/perl
#
# Copyright 2012 Chris Wilson
# Copyright 2006 Holger Levsen
#
# This plugin monitors ALL processes on a system. No exceptions. It can
# produce very big graphs! But if you want to know where your CPU time
# is going without knowing what to monitor in advance, this can help;
# or in addition to one of the more specific CPU plugins to monitor
# just Apache or MySQL, for example.
#
# It's not obvious what the graph heights actually mean, even to me.
# Each counter is a DERIVE (difference since the last counter reading)
# of the CPU time usage (in seconds) accounted to each process, summed
# by the process name, so all Apache and all MySQL processes are grouped
# together. Processes with no CPU usage at all are ignored. Processes
# that die may not appear on the graph, and anyway their last chunk of
# CPU usage before they died is lost. You could modify this plugin to
# read SAR/psacct records if you care about that.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#scriptname=`basename $0`
#vsname=`echo $scriptname | perl -ne '/^vserver_proc_VM_(.*)/ and print $1'`
#if [ "$1" = "suggest" ]; then
# ls -1 /etc/vservers
# exit 0
#elif [ -z "$vsname" ]; then
# echo "Must be used with a vserver name; try '$0 suggest'" >&2
# exit 2
#fi
use strict;
use warnings;
my $cmd = "ps -eo time,comm h";
open PS, "$cmd|" or die "Failed to run ps command: $cmd: $!";
# my $header_line = <PS>;
my %total_cpu_by_process;
while (<PS>)
{
my @fields = split;
my $cputime = $fields[0];
my $process = $fields[1];
# remove any / and everything after it from the process name,
# e.g. kworker/0:2 -> kworker
$process =~ s|/.*||;
# remove any . at the end of the name (why does this appear?)
# $process =~ s|\.$||;
# change any symbol that's not allowed in a munin variable name to _
$process =~ tr|a-zA-Z0-9|_|c;
my @times = split /:/, $cputime;
my @days = split /-/, $times[0];
if ($days[1]) {
$cputime = ((($days[0] * 24) + $days[1]) * 60 + $times[1]) * 60 + $times[2];
} else {
$cputime = (($times[0] * 60) + $times[1]) * 60 + $times[2];
}
$total_cpu_by_process{$process} += $cputime;
}
foreach my $process (keys %total_cpu_by_process)
{
# remove all processes with 0 cpu time
if (not $total_cpu_by_process{$process})
{
delete $total_cpu_by_process{$process};
}
}
close(PS);
if (@ARGV and $ARGV[0] eq "config")
{
print <<END;
graph_title CPU time by Process
graph_args --base 1000
graph_vlabel seconds
graph_category processes
graph_info Shows CPU time used by each process name
END
my $stack = 0;
sub draw() { return $stack++ ? "STACK" : "AREA" }
print map
{
"$_.label $_\n" .
"$_.min 0\n" .
"$_.type DERIVE\n" .
"$_.draw " . draw() . "\n"
} sort keys %total_cpu_by_process;
}
else
{
print map
{
"$_.value $total_cpu_by_process{$_}\n"
} sort keys %total_cpu_by_process;
}
exit(0);

92
plugins/cpu/multicpu1sec Normal file
View file

@ -0,0 +1,92 @@
#!/bin/bash
# (c) 2012 - Bushmills
# License : GPLv2
#%# family=auto
#%# capabilities=autoconf
interval=1 # mpstat sampling interval
timeout=1200 # 20 minutes daemon watchdog timeout
watchdog=60 # test for timeout every $watchdog seconds
pluginfull="$0" # full name of plugin
plugin="${0##*/}" # name of plugin
pidfile="$MUNIN_PLUGSTATE/munin.$plugin.pid"
cache="$MUNIN_PLUGSTATE/munin.$plugin.value"
graph="$plugin"
section="system:cpu"
style="AREA"
cpus=$(grep -c ^processor /proc/cpuinfo)
run_watchdog() { # should also trap kill and term signals
kill -0 $(cat $pidfile) 2> /dev/null || rm -f $pidfile
}
run_acquire() {
echo "$$" > $pidfile
LANG=C mpstat -P ALL $interval |
awk -v cpus=$cpus '$2>=0&&$2<99 {print $2, systime(), (100-$11)/cpus; system("");}' >> $cache
rm -f $pidfile $cache
}
run_daemon() {
run_watchdog
if [ -f $pidfile ]; then
touch $pidfile
else
$pluginfull acquire &
fi
}
# --------------------------------------------------------------------------
run_autoconf() {
run=(yes no)
type -t mpstat > /dev/null
echo "${run[$?]}"
}
run_config() {
run_daemon
cat << EOF
graph_title $graph
graph_category $section
graph_vlabel average cpu use %
graph_scale no
graph_total All CPUs
update_rate 1
graph_data_size custom 1d, 10s for 1w, 1m for 1t, 5m for 1y
EOF
cpun=0
for ((i=0; i<$cpus; i++)); do
cat << EOF
cpu${cpun}.label CPU $cpun
cpu${cpun}.draw $style
cpu${cpun}.min 0
EOF
style=STACK
((cpun++))
done
}
run_fetch() {
run_daemon
awk 'NF==3 {print "cpu" $1 ".value " $2 ":" $3}' $cache
> $cache
}
run_${1:-fetch}
exit 0
# export -f functionname export functionname to subshell, avoiding the need
# for locating plugin for subshell calling, when process needs a different
# pid. Instead, $SHELL -c functionname can be used. useful for calling
# acquire which needs a different pid than watchdog, otherwise watchdog
# could/will kill itself when expiring before the watched process is killed.
# not a POSIX feature.
# for Munin Plugin Gallery
# graph_category 1sec

View file

@ -0,0 +1,221 @@
/*
* multicpu1sec C plugin
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/file.h>
#define PROC_STAT "/proc/stat"
int fail(char* msg) {
perror(msg);
return 1;
}
int config() {
/* Get the number of CPU */
int f;
if ( !(f=open(PROC_STAT, O_RDONLY)) ) {
return fail("cannot open " PROC_STAT);
}
// Starting with -1, since the first line is the "global cpu line"
int ncpu = -1;
const int buffer_size = 64 * 1024;
char buffer[buffer_size];
// whole /proc/stat can be read in 1 syscall
if (read(f, buffer, buffer_size) <= 0) {
return fail("cannot read " PROC_STAT);
}
// tokenization per-line
char* line;
char* newl = "\n";
for (line = strtok(buffer, newl); line; line = strtok(NULL, newl)) {
if (! strncmp(line, "cpu", 3)) ncpu ++;
}
close(f);
printf(
"graph_title multicpu1sec\n"
"graph_category system::1sec\n"
"graph_vlabel average cpu use %%\n"
"graph_scale no\n"
"graph_total All CPUs\n"
"update_rate 1\n"
"graph_data_size custom 1d, 10s for 1w, 1m for 1t, 5m for 1y\n"
);
int i;
for (i = 0; i < ncpu; i++) {
printf("cpu%d.label CPU %d\n", i, i);
printf("cpu%d.draw %s\n", i, "AREASTACK");
printf("cpu%d.type %s\n", i, "DERIVE");
printf("cpu%d.min 0\n", i);
}
return 0;
}
char* pid_filename;
char* cache_filename;
/* Wait until the next second, and return the EPOCH */
time_t wait_until_next_second() {
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
time_t current_epoch = tp.tv_sec;
long nsec_to_sleep = 1000*1000*1000 - tp.tv_nsec;
/* Only sleep if needed */
if (nsec_to_sleep > 0) {
tp.tv_sec = 0;
tp.tv_nsec = nsec_to_sleep;
nanosleep(&tp, NULL);
}
return current_epoch + 1;
}
int acquire() {
/* fork ourselves if not asked otherwise */
char* no_fork = getenv("no_fork");
if (! no_fork || strcmp("1", no_fork)) {
if (fork()) return;
// we are the child, complete the daemonization
/* Close standard IO */
fclose(stdin);
fclose(stdout);
fclose(stderr);
/* create new session and process group */
setsid();
}
/* write the pid */
FILE* pid_file = fopen(pid_filename, "w");
fprintf(pid_file, "%d\n", getpid());
fclose(pid_file);
/* Reading /proc/stat */
int f = open(PROC_STAT, O_RDONLY);
/* open the spoolfile */
int cache_file = open(cache_filename, O_CREAT | O_APPEND | O_WRONLY);
/* loop each second */
while (1) {
/* wait until next second */
time_t epoch = wait_until_next_second();
const int buffer_size = 64 * 1024;
char buffer[buffer_size];
if (lseek(f, 0, SEEK_SET) < 0) {
return fail("cannot seek " PROC_STAT);
}
// whole /proc/stat can be read in 1 syscall
if (read(f, buffer, buffer_size) <= 0) {
return fail("cannot read " PROC_STAT);
}
// ignore the 1rst line
char* line;
const char* newl = "\n";
line = strtok(buffer, newl);
/* lock */
flock(cache_file, LOCK_EX);
for (line = strtok(NULL, newl); line; line = strtok(NULL, newl)) {
// Not on CPU lines anymore
if (strncmp(line, "cpu", 3)) break;
char cpu_id[64];
long usr, nice, sys, idle, iowait, irq, softirq;
sscanf(line, "%s %ld %ld %ld %ld %ld %ld %ld", cpu_id, &usr, &nice, &sys, &idle, &iowait, &irq, &softirq);
long used = usr + nice + sys + iowait + irq + softirq;
char out_buffer[1024];
sprintf(out_buffer, "%s.value %ld:%ld\n", cpu_id, epoch, used);
write(cache_file, out_buffer, strlen(out_buffer));
}
/* unlock */
flock(cache_file, LOCK_UN);
}
close(cache_file);
close(f);
return 0;
}
int fetch() {
FILE* cache_file = fopen(cache_filename, "r+");
/* lock */
flock(fileno(cache_file), LOCK_EX);
/* cat the cache_file to stdout */
char buffer[1024];
while (fgets(buffer, 1024, cache_file)) {
printf("%s", buffer);
}
ftruncate(fileno(cache_file), 0);
fclose(cache_file);
return 0;
}
int main(int argc, char **argv) {
/* resolve paths */
char *MUNIN_PLUGSTATE = getenv("MUNIN_PLUGSTATE");
/* Default is current directory */
if (! MUNIN_PLUGSTATE) MUNIN_PLUGSTATE = ".";
size_t MUNIN_PLUGSTATE_length = strlen(MUNIN_PLUGSTATE);
pid_filename = malloc(MUNIN_PLUGSTATE_length + strlen("/multicpu1sec.") + strlen("pid") + 1); pid_filename[0] = '\0';
cache_filename = malloc(MUNIN_PLUGSTATE_length + strlen("/multicpu1sec.") + strlen("value") + 1); cache_filename[0] = '\0';
strcat(pid_filename, MUNIN_PLUGSTATE);
strcat(pid_filename, "/multicpu1sec.pid");
strcat(cache_filename, MUNIN_PLUGSTATE);
strcat(cache_filename, "/multicpu1sec.value");
if (argc > 1) {
char* first_arg = argv[1];
if (! strcmp(first_arg, "config")) {
return config();
}
if (! strcmp(first_arg, "acquire")) {
return acquire();
}
}
return fetch();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB