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 	static final String JSON_TAG_DATA = "data";
34 	static final String JSON_TAG_ERROR = "error";
35 	static final String JSON_TAG_KEYS = "keys";
36 	static final String JSON_TAG_PASSWORD = "password";
37 	static final String JSON_TAG_REQUEST = "request";
38 	static final String JSON_TAG_RESPONSE = "response";
39 	static final String JSON_TAG_USERNAME = "username";
40 	static final String JSON_TAG_VALUE = "value";
41 	static final String JSON_TAG_JMX_ENDPOINT = "jmx_endpoint";
42 
43 	static final String JSON_REQUEST_INTERNAL = "java gateway internal";
44 	static final String JSON_REQUEST_JMX = "java gateway jmx";
45 
46 	static final String JSON_RESPONSE_FAILED = "failed";
47 	static final String JSON_RESPONSE_SUCCESS = "success";
48 
49 	protected JSONObject request;
50 	protected ArrayList<String> keys;
51 
ItemChecker(JSONObject request)52 	protected ItemChecker(JSONObject request) throws ZabbixException
53 	{
54 		this.request = request;
55 
56 		try
57 		{
58 			JSONArray jsonKeys = request.getJSONArray(JSON_TAG_KEYS);
59 			keys = new ArrayList<String>();
60 
61 			for (int i = 0; i < jsonKeys.length(); i++)
62 				keys.add(jsonKeys.getString(i));
63 		}
64 		catch (Exception e)
65 		{
66 			throw new ZabbixException(e);
67 		}
68 	}
69 
getValues()70 	JSONArray getValues() throws ZabbixException
71 	{
72 		JSONArray values = new JSONArray();
73 
74 		for (String key : keys)
75 			values.put(getJSONValue(key));
76 
77 		return values;
78 	}
79 
getFirstKey()80 	String getFirstKey()
81 	{
82 		return 0 == keys.size() ? null : keys.get(0);
83 	}
84 
getJSONValue(String key)85 	protected final JSONObject getJSONValue(String key)
86 	{
87 		JSONObject value = new JSONObject();
88 
89 		try
90 		{
91 			logger.debug("getting value for item '{}'", key);
92 			String text = getStringValue(key);
93 			logger.debug("received value '{}' for item '{}'", text, key);
94 			value.put(JSON_TAG_VALUE, text);
95 		}
96 		catch (Exception e1)
97 		{
98 			try
99 			{
100 				logger.debug("caught exception for item '{}'", key, e1);
101 				value.put(JSON_TAG_ERROR, ZabbixException.getRootCauseMessage(e1));
102 			}
103 			catch (JSONException e2)
104 			{
105 				Object[] logInfo = {JSON_TAG_ERROR, e1.getMessage(), ZabbixException.getRootCauseMessage(e2)};
106 				logger.warn("cannot add JSON attribute '{}' with message '{}': {}", logInfo);
107 				logger.debug("error caused by", e2);
108 			}
109 		}
110 
111 		return value;
112 	}
113 
getStringValue(String key)114 	protected abstract String getStringValue(String key) throws Exception;
115 }
116