/*************************************************************************** * main.c * * Sun Jun 3 13:27:11 2007 * Copyright 2007 User * Email ****************************************************************************/ /* * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #define STATE_OK 0 #define STATE_WARNING 1 #define STATE_CRITICAL 2 #define STATE_UNKNOWN 3 // Fonction de sortie du programme avec formattage chaine + code retour int goout(char* pgm_name, char* message, int status) { char status_char[10]; if(status==STATE_OK) strcpy(status_char,"OK"); else if(status==STATE_WARNING) strcpy(status_char,"WARNING"); else if(status==STATE_CRITICAL) strcpy(status_char,"CRITICAL"); else if(status==STATE_UNKNOWN) { strcpy(status_char,"UNKNOWN"); usage(pgm_name); } printf("[%s] %s : %s\n",status_char,pgm_name,message); exit(status); } // Fonction d'aide à l'utilisation du programme int usage(char* pgm_name) { printf("USAGE : %s (save|read) filename [warning] [critical]\n\n",pgm_name); printf("Cet outil permet de vérifier le bon fonctionnement d'un système d'ordonnancement\nafin de lever des alertes pour NAGIOS (ex. crontab unix, taches planifiées windows, ...)\n\n"); printf("* Pour ecrire dans un fichier le timestamp courant : %s save nom_fichier_temporaire\n",pgm_name); printf("* Pour comparer le timestamp courant avec le timestamp enregistré\nlors de la dernière exécution en mode 'save'\n: %s read nom_fichier_temporaire seuil_attention seuil_critique\n\n",pgm_name); } int main(int argc, char *argv[]) { time_t t; time_t old_t; FILE *fp; int intervalle; int intervalle_max; int intervalle_warn; char output[100]; //vérification du nombre d'argument spécifié if(argc<2) { goout(argv[0],"Des arguments manquent à l'exécution du programme",STATE_UNKNOWN); } else if(!(strcmp(argv[1],"save"))) { // Mode écriture fichier //vérification du nombre d'arguments if (argc<3) goout(argv[0],"Des arguments manquent à l'exécution du programme",STATE_UNKNOWN); else { // Si OK, ecriture dans le fichier spécifié du timestamp if((fp=fopen(argv[2],"w")) == NULL) goout(argv[0],"impossible d'ouvrir le fichier temporaire",STATE_UNKNOWN); time(&t); fprintf(fp,"%ld", t); fclose(fp); } } else if(!(strcmp(argv[1],"read"))) { // Mode lecture-comparaison //vérification du nombre d'arguments if (argc<5) goout(argv[0],"Des arguments manquent à l'exécution du programme",STATE_UNKNOWN); else { //Si OK : lecture dans le fichier spécifié du timestamp de la dernière écriture (mode save) if((fp=fopen(argv[2],"r")) == NULL) goout(argv[0],"impossible d'ouvrir le fichier temporaire",STATE_UNKNOWN); fscanf(fp,"%ld", &old_t); fclose(fp); time(&t); // Comparaison du timestamp actuel avec celui enregistré lors de la dernière écriture (mode save) intervalle=(int)(t-old_t); // Lecture des seuils critique et warning à prendre en compte dans NAGIOS intervalle_max=atoi(argv[4]); intervalle_warn=atoi(argv[3]); // Génération des alertes message + sortie if (intervalle>intervalle_max) { sprintf(output,"Seuil dépassé dernier traitement : %i sec./%i sec. max.",intervalle,intervalle_max); goout(argv[0],output,STATE_CRITICAL); } else if(intervalle>intervalle_warn) { sprintf(output,"Seuil dépassé dernier traitement : %i sec./%i sec. max.",intervalle,intervalle_warn); goout(argv[0],output,STATE_WARNING); } } } return 0; }