summaryrefslogtreecommitdiffstats
path: root/web/attachments/182402-check_mysql.c
blob: 5ef37d17a92735b9c4fb500645e274090d14eabb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*****************************************************************
 *
 * Program: check_mysql.c
 * License: GPL
 *
 * Written by Tim Weippert
 *   (based on plugins by Ethan Galstad and MySQL example code)
 *
 * Adapted by J. Javier Sianes - skyo@rotxa.org
 *
 *
 * Command line: check_mysql <host> [user] [passwd] [db] [port]
 *      <host> can be the FQDN or the IP-Adress
 *      [user], [passwd], [db] and [port] are optional
 *
 * Description:
 *
 * This plugin attempts to connect to an MySQL Server
 * with the optional specified parameters user, passwd and db.
 * Normaly the host and a user HAVE to assigned.
 *
 * The plugin returns
 * STATE_OK and the Version Number of the Server when all is fine
 * STATE_CRITICAL if the Connection can't be esablished
 * STATE_WARNING if the connection was established but the
 * program can't get the Versoin Number
 * STATE_UNKNOWN if to many parameters are given
 *
 * Copyright (c) 1999 by Tim Weippert
 *
 * Changes:
 *   16.12.1999: Changed the return codes from numbers to statements
 *   20.06.2006: Included new db and port parameters
 *   20.06.2006: Adapted for using new MySQL5 API
 *
 *******************************************************************/

/*****************************************************************
 *
 * Note that all includes are related to Nagios and MySQL default installation.
 * If you have installed them on a different location, you may change
 * the following include lines in order to make it works.
 *
 * To compile the agent, in a shell use the following command, for example:
 *
 * gcc -lmysqlclient -o check_mysql check_mysql.c
 *
 *******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "/usr/include/nagios/config.h"
#include "/usr/include/nagios/common.h"
#include "/usr/include/nagios/nagios.h"
#include "/usr/include/mysql/mysql.h"

MYSQL mysql;

int main(int argc, char **argv)
{
  uint i = 0;
  int mport;
  char *host;
  char *user;
  char *passwd;
  char *db;

  char *status;
  char *version;

  if ( ( argc > 6 ) || ( argc < 2 ) ) {
    printf("Incorrect number of arguments supplied - %i .\n", argc);
    printf("Usage: %s <host> [user] [passwd] [db] [port]\n", argv[0]);
    return STATE_UNKNOWN;
  }

  (host = argv[1]) || (host = NULL);
  (user = argv[2]) || (user = NULL);
  (passwd = argv[3]) || (passwd = NULL);
  (db = argv[4]) || (db = "mysql");
  if (argc==6) { mport = atoi(argv[5]); } else { mport = 3306;  }

  mysql_init(&mysql);
  mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"check_mysql");

  if (!mysql_real_connect(&mysql,host,user,passwd,db,(unsigned int)mport,NULL,0)) {
    printf("Connect ERROR, Failed to connect to database '%s': %s\n",db,mysql_error(&mysql));
    return STATE_CRITICAL;
  }

  if ( !(version = mysql_get_server_info(&mysql)) ) {
    printf("Connect OK, but can't get Serverinfo ... something wrong !\n");
    return STATE_WARNING;
  }

  printf("MYSQL OK - Running Version: %s\n", version);

  mysql_close(&mysql);
  return STATE_OK;
}