1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 package com.zabbix.gateway;
21 
22 import java.util.ArrayList;
23 
24 import org.json.*;
25 
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 
29 abstract class ItemChecker
30 {
31 	private static final Logger logger = LoggerFactory.getLogger(ItemChecker.class);
32 
33 	public static final String JSON_TAG_CONN = "conn";
34 	public static final String JSON_TAG_DATA = "data";
35 	public static final String JSON_TAG_ERROR = "error";
36 	public static final String JSON_TAG_KEYS = "keys";
37 	public static final String JSON_TAG_PASSWORD = "password";
38 	public static final String JSON_TAG_PORT = "port";
39 	public static final String JSON_TAG_REQUEST = "request";
40 	public static final String JSON_TAG_RESPONSE = "response";
41 	public static final String JSON_TAG_USERNAME = "username";
42 	public static final String JSON_TAG_VALUE = "value";
43 
44 	public static final String JSON_REQUEST_INTERNAL = "java gateway internal";
45 	public static final String JSON_REQUEST_JMX = "java gateway jmx";
46 
47 	public static final String JSON_RESPONSE_FAILED = "failed";
48 	public static final String JSON_RESPONSE_SUCCESS = "success";
49 
50 	protected JSONObject request;
51 	protected ArrayList<String> keys;
52 
ItemChecker(JSONObject request)53 	protected ItemChecker(JSONObject request) throws ZabbixException
54 	{
55 		this.request = request;
56 
57 		try
58 		{
59 			JSONArray jsonKeys = request.getJSONArray(JSON_TAG_KEYS);
60 			keys = new ArrayList<String>();
61 
62 			for (int i = 0; i < jsonKeys.length(); i++)
63 				keys.add(jsonKeys.getString(i));
64 		}
65 		catch (Exception e)
66 		{
67 			throw new ZabbixException(e);
68 		}
69 	}
70 
getValues()71 	public JSONArray getValues() throws ZabbixException
72 	{
73 		JSONArray values = new JSONArray();
74 
75 		for (String key : keys)
76 			values.put(getJSONValue(key));
77 
78 		return values;
79 	}
80 
getJSONValue(String key)81 	protected final JSONObject getJSONValue(String key)
82 	{
83 		JSONObject value = new JSONObject();
84 
85 		try
86 		{
87 			logger.debug("getting value for item '{}'", key);
88 			String text = getStringValue(key);
89 			logger.debug("received value '{}' for item '{}'", text, key);
90 			value.put(JSON_TAG_VALUE, text);
91 		}
92 		catch (Exception e1)
93 		{
94 			try
95 			{
96 				logger.debug("caught exception for item '{}'", key, e1);
97 				value.put(JSON_TAG_ERROR, e1.getMessage());
98 			}
99 			catch (JSONException e2)
100 			{
101 				Object[] logInfo = {JSON_TAG_ERROR, e1.getMessage(), e2};
102 				logger.warn("cannot add JSON attribute '{}' with message '{}'", logInfo);
103 			}
104 		}
105 
106 		return value;
107 	}
108 
getStringValue(String key)109 	protected abstract String getStringValue(String key) throws Exception;
110 }
111