View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.plugin.mysql;
17  
18  import it.jnrpe.ICommandLine;
19  import it.jnrpe.Status;
20  import it.jnrpe.plugins.Metric;
21  import it.jnrpe.plugins.MetricGatheringException;
22  import it.jnrpe.plugins.PluginBase;
23  import it.jnrpe.utils.BadThresholdException;
24  import it.jnrpe.utils.thresholds.ThresholdsEvaluatorBuilder;
25  
26  import java.math.BigDecimal;
27  import java.sql.Connection;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Statement;
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.List;
34  
35  /**
36   * Plugin that checks a mysql query result against threshold levels.
37   *
38   * @author Frederico Campos
39   */
40  public class CheckMysqlQuery extends PluginBase {
41  
42  	@Override
43  	public void configureThresholdEvaluatorBuilder(
44  			ThresholdsEvaluatorBuilder thrb, ICommandLine cl)
45  					throws BadThresholdException {
46  
47  		if (cl.hasOption("th")) {
48  			super.configureThresholdEvaluatorBuilder(thrb, cl);
49  		} else {
50  			thrb.withLegacyThreshold("rows", null,
51  					cl.getOptionValue("warning"), cl.getOptionValue("critical"));
52  		}
53  
54  	}
55  
56  	/**
57  	 * Execute and gather metrics
58  	 */
59  	public Collection<Metric> gatherMetrics(ICommandLine cl) throws MetricGatheringException {
60  		log.debug("check_mysql_query gather metrics");
61  		List<Metric> metrics = new ArrayList<Metric>();
62  		Mysql mysql = new Mysql(cl);
63  		Connection conn = null;
64  		try {
65  			conn = mysql.getConnection();
66  		} catch (ClassNotFoundException e) {
67  			log.error("Mysql driver library not found into the classpath"
68  					+ ": download and put it in the same directory "
69  					+ "of this plugin");
70  			throw new MetricGatheringException("CHECK_MYSQL_QUERY - CRITICAL: Error accessing the "
71  					+ "MySQL server - JDBC driver not installed", Status.CRITICAL, e);
72  		} catch (Exception e) {
73  			log.error("Error accessing the MySQL server", e);
74  			throw new MetricGatheringException("CHECK_MYSQL_QUERY - CRITICAL: Error accessing "
75  					+ "the MySQL server - " + e.getMessage(), Status.CRITICAL,e);
76  		}
77  
78  		String query = cl.getOptionValue("query");
79  		Statement st = null;
80  		ResultSet set = null;
81  		try {
82  			st = conn.createStatement();
83  			st.execute(query);
84  			set = st.getResultSet();
85  			BigDecimal value = null;
86  			if (set.first()) {
87  				value = set.getBigDecimal(1);
88  			}
89  
90  			metrics.add(new Metric("rows", "CHECK_MYSQL_QUERY - Returned value is " + 
91  					(value != null ? value.longValue() : null) , 
92  					value, 
93  					null, 
94  					null));
95  
96  		} catch (SQLException e) {
97  			log.warn("Error executing plugin CheckMysqlQuery : " + e.getMessage(), e);
98  			throw new MetricGatheringException("CHECK_MYSQL_QUERY - CRITICAL: " + e.getMessage(), Status.CRITICAL, e);
99  		} finally {
100 			if (st != null) {
101 				try {
102 					st.close();
103 				} catch (SQLException e) {
104 					log.error("Error closing MySQL statement", e);
105 				}
106 			}
107 			if (set != null) {
108 				try {
109 					set.close();
110 				} catch (SQLException e) {
111 					log.error("Error closing MySQL ResultSet", e);
112 				}
113 			}
114 			mysql.closeConnection(conn);
115 		}
116 
117 		return metrics;
118 	}
119 
120 
121 	@Override
122 	protected String getPluginName() {
123 		return "CHECK_MYSQL_QUERY";
124 	}
125 }