Quantcast
Channel: SUCKUP.de » STATE
Viewing all articles
Browse latest Browse all 2

C-Programm – check_mem für Nagios

$
0
0

Habe gerade ein kleines C-Programm geschrieben, welches mithilfe von Nagios / Icinga die Speicherauslastung (Arbeitsspeicher + Swap) eines Linux-Servers überprüfen kann. Wenn ein Server seine aktuell laufenden Programm auf der Festplatte auslagern muss, da zu wenig Arbeitsspeicher zur Verfügung steht, sollte man darüber ggf. informiert werden… :-)

/*****************************************************************************
* 
* Nagios check_mem plugin
* 
* License: GPL
* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
* Copyright (c) 2000-2007 Nagios Plugins Development Team
* Copyright (c) 2011 Lars Moelleken (voku@voku-online.de)
* 
* Description:
* 
* This file contains the check_mem plugin
* 
* 
* 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, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* 
*****************************************************************************/

const char *progname = "check_mem";
const char *NP_VERSION = "0.1";
const char *copyright = "2011";
const char *email = "voku@voku-online.de";

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sysinfo.h>

void print_usage (void);
void print_help (void);


int main (int argc, char *argv[]) {
    long total = 0, free_total = 0, used_total = 0;
    long total_mem = 0, free_mem = 0, used_mem = 0;
    long total_swap = 0, free_swap = 0, used_swap = 0;
    struct sysinfo meminfo;

    char *warn_percent = NULL;
    char *crit_percent = NULL;

    int STATE_OK = 0;
    int STATE_WARNING = 1;
    int STATE_CRITICAL = 2;
    int STATE_UNKNOWN = 3;
    int result = 0;

     if (argc < 2) {
        print_help();
        printf("Hilfe anzeigen lassen: %s -h\n", argv[0]);
        exit(STATE_UNKNOWN);
    }

    char option;
    while((option = getopt(argc, argv, "hwc:")) != EOF) {
        switch(option) {
            case 'h':
                print_help();
                break;
            case 'w':
                warn_percent = argv[2];
                break;
            case 'c':
                crit_percent = argv[4];
                break;
            case ':':
                printf("option needs a value\n");
                break;
            case '?':
                printf("unknown option: %c\n", optopt);
                break;
        }
    }

    if(sysinfo(&meminfo) !=0) {
        perror("Fehler ...");
    }

    total_mem = meminfo.totalram;
    free_mem = meminfo.freeram;
    used_mem = meminfo.totalram - meminfo.freeram;

    total_swap = meminfo.totalswap;
    free_swap = meminfo.freeswap;
    used_swap = meminfo.totalswap - meminfo.freeswap;

    total = total_mem + total_swap;
    free_total = free_mem + free_swap;
    used_total = used_mem + used_swap;

    if (free_swap < total_swap/2 || free_total < total / 100 * atoi(crit_percent)) {
        printf("CRITICAL > ");
        result = STATE_CRITICAL;
    } else if (free_total < total / 100 * atoi(warn_percent)) {
        printf("WARNING > ");
        result = STATE_WARNING;
    } else {
        result = STATE_OK;
        printf("OK > ");
    }
    printf("Free Mem+Swap: %ld - Free Mem: %ld - Free Swap: %ld", free_total/1024/1024, free_mem/1024/1024, free_swap/1024/1024); // test-Ausgabe
    return result;
}


void print_help (void) {
    printf ("%s\n", "Check memory on local machine.");

  printf ("\n");

    print_usage ();

  printf (" %s\n", "-w PERCENT%%");
  printf ("    %s\n", "Exit with WARNING status if less than PERCENT of mem/swap space is free");
  printf (" %s\n", "-c PERCENT%%");
  printf ("    %s\n", "Exit with CRITCAL status if less than PERCENT of mem/swap space is free");
    printf ("\n");
}


void print_usage (void) {
  printf ("%s -w <percent_free>%% -c <percent_free>%%\n",progname);
}

Viewing all articles
Browse latest Browse all 2

Latest Images

Pangarap Quotes

Pangarap Quotes

Vimeo 10.7.0 by Vimeo.com, Inc.

Vimeo 10.7.0 by Vimeo.com, Inc.

HANGAD

HANGAD

MAKAKAALAM

MAKAKAALAM

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC

Trending Articles


Imágenes de Robin Hood para colorear


Dino Rey para colorear


Libros para colorear


Mandalas de flores para colorear


Dibujos para colorear de perros


Toro para colorear


People Walk Away Quotes, Inspire Quotes


Long Distance Relationship Tagalog Love Quotes


Love Quotes Tagalog


RE: Mutton Pies (mely)


Pokemon para colorear


Winx Club para colorear


Girasoles para colorear


Sapos para colorear


Renos para colorear


Dromedario para colorear


Inspirational Tagalog quotes and Motivational English Quotes


Mga patama Quotes tagalog


Tamis Ng tagumpay


Mga Tala sa “Unang Siglo ng Nobela sa Filipinas” (2009) ni Virgilio S. Almario





Latest Images

Pangarap Quotes

Pangarap Quotes

Vimeo 10.7.0 by Vimeo.com, Inc.

Vimeo 10.7.0 by Vimeo.com, Inc.

HANGAD

HANGAD

MAKAKAALAM

MAKAKAALAM

Doodle Jump 3.11.30 by Lima Sky LLC

Doodle Jump 3.11.30 by Lima Sky LLC