@Test public void failWhenAddressIsWrong() { ArrayList<Metric> testMetrics = new ArrayList<Metric>(); Metric mockedMetric = mock(Metric.class); testMetrics.add(mockedMetric); Assert.assertFalse(publisher.flush(Collections.singletonList(testMetrics))); verify(mockedMetric, times(0)).getName(); verify(mockedMetric, times(0)).getValue(); }
@Override public boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics) { Socket zabbixConnection = null; OutputStream out = null; BufferedReader in = null; try { for (List<Metric> metrics : listOfMetrics) { for (Metric metric : metrics) { zabbixConnection = new Socket(zabbixServer, zabbixPort); StringBuilder message = new StringBuilder(); message.append(MESSAGE_START); message.append(host); message.append(MESSAGE_MIDDLE_LEFT); message.append(metric.getName().replaceAll("\\s", "").replace(":", "")); message.append(MESSAGE_MIDDLE_RIGHT); message.append(metric.getValue()); message.append(MESSAGE_END); String s = message.toString(); byte[] chars = s.getBytes(); int length = chars.length; out = zabbixConnection.getOutputStream(); out.write(new byte[]{ 'Z', 'B', 'X', 'D', '\1', (byte) (length & BITMASK), (byte) ((length >> SHIFT_TWO) & SHIFT_TWO_BITMASK), (byte) ((length >> SHIFT_FOUR) & SHIFT_FOUR_BITMASK), (byte) ((length >> SHIFT_SIX) & SHIFT_SIX_BITMASK), '\0', '\0', '\0', '\0'}); out.write(chars); out.flush(); in = new BufferedReader(new InputStreamReader(zabbixConnection.getInputStream())); LOGGER.debug("Message sent to Zabbix: " + message.toString()); } } } catch (IOException e) { LOGGER.warn("Failed to establish connection to Zabbix", e); return false; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbixConnection != null) { zabbixConnection.close(); } } catch (IOException e) { } } return true; }
ZabbixMonitorPublisher extends BufferedHandler<ArrayList<Metric>> { @Override public boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics) { Socket zabbixConnection = null; OutputStream out = null; BufferedReader in = null; try { for (List<Metric> metrics : listOfMetrics) { for (Metric metric : metrics) { zabbixConnection = new Socket(zabbixServer, zabbixPort); StringBuilder message = new StringBuilder(); message.append(MESSAGE_START); message.append(host); message.append(MESSAGE_MIDDLE_LEFT); message.append(metric.getName().replaceAll("\\s", "").replace(":", "")); message.append(MESSAGE_MIDDLE_RIGHT); message.append(metric.getValue()); message.append(MESSAGE_END); String s = message.toString(); byte[] chars = s.getBytes(); int length = chars.length; out = zabbixConnection.getOutputStream(); out.write(new byte[]{ 'Z', 'B', 'X', 'D', '\1', (byte) (length & BITMASK), (byte) ((length >> SHIFT_TWO) & SHIFT_TWO_BITMASK), (byte) ((length >> SHIFT_FOUR) & SHIFT_FOUR_BITMASK), (byte) ((length >> SHIFT_SIX) & SHIFT_SIX_BITMASK), '\0', '\0', '\0', '\0'}); out.write(chars); out.flush(); in = new BufferedReader(new InputStreamReader(zabbixConnection.getInputStream())); LOGGER.debug("Message sent to Zabbix: " + message.toString()); } } } catch (IOException e) { LOGGER.warn("Failed to establish connection to Zabbix", e); return false; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbixConnection != null) { zabbixConnection.close(); } } catch (IOException e) { } } return true; } }
ZabbixMonitorPublisher extends BufferedHandler<ArrayList<Metric>> { @Override public boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics) { Socket zabbixConnection = null; OutputStream out = null; BufferedReader in = null; try { for (List<Metric> metrics : listOfMetrics) { for (Metric metric : metrics) { zabbixConnection = new Socket(zabbixServer, zabbixPort); StringBuilder message = new StringBuilder(); message.append(MESSAGE_START); message.append(host); message.append(MESSAGE_MIDDLE_LEFT); message.append(metric.getName().replaceAll("\\s", "").replace(":", "")); message.append(MESSAGE_MIDDLE_RIGHT); message.append(metric.getValue()); message.append(MESSAGE_END); String s = message.toString(); byte[] chars = s.getBytes(); int length = chars.length; out = zabbixConnection.getOutputStream(); out.write(new byte[]{ 'Z', 'B', 'X', 'D', '\1', (byte) (length & BITMASK), (byte) ((length >> SHIFT_TWO) & SHIFT_TWO_BITMASK), (byte) ((length >> SHIFT_FOUR) & SHIFT_FOUR_BITMASK), (byte) ((length >> SHIFT_SIX) & SHIFT_SIX_BITMASK), '\0', '\0', '\0', '\0'}); out.write(chars); out.flush(); in = new BufferedReader(new InputStreamReader(zabbixConnection.getInputStream())); LOGGER.debug("Message sent to Zabbix: " + message.toString()); } } } catch (IOException e) { LOGGER.warn("Failed to establish connection to Zabbix", e); return false; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbixConnection != null) { zabbixConnection.close(); } } catch (IOException e) { } } return true; } @Inject  ZabbixMonitorPublisher();  ZabbixMonitorPublisher(OnOffSwitch enabledSwitch); }
ZabbixMonitorPublisher extends BufferedHandler<ArrayList<Metric>> { @Override public boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics) { Socket zabbixConnection = null; OutputStream out = null; BufferedReader in = null; try { for (List<Metric> metrics : listOfMetrics) { for (Metric metric : metrics) { zabbixConnection = new Socket(zabbixServer, zabbixPort); StringBuilder message = new StringBuilder(); message.append(MESSAGE_START); message.append(host); message.append(MESSAGE_MIDDLE_LEFT); message.append(metric.getName().replaceAll("\\s", "").replace(":", "")); message.append(MESSAGE_MIDDLE_RIGHT); message.append(metric.getValue()); message.append(MESSAGE_END); String s = message.toString(); byte[] chars = s.getBytes(); int length = chars.length; out = zabbixConnection.getOutputStream(); out.write(new byte[]{ 'Z', 'B', 'X', 'D', '\1', (byte) (length & BITMASK), (byte) ((length >> SHIFT_TWO) & SHIFT_TWO_BITMASK), (byte) ((length >> SHIFT_FOUR) & SHIFT_FOUR_BITMASK), (byte) ((length >> SHIFT_SIX) & SHIFT_SIX_BITMASK), '\0', '\0', '\0', '\0'}); out.write(chars); out.flush(); in = new BufferedReader(new InputStreamReader(zabbixConnection.getInputStream())); LOGGER.debug("Message sent to Zabbix: " + message.toString()); } } } catch (IOException e) { LOGGER.warn("Failed to establish connection to Zabbix", e); return false; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbixConnection != null) { zabbixConnection.close(); } } catch (IOException e) { } } return true; } @Inject  ZabbixMonitorPublisher();  ZabbixMonitorPublisher(OnOffSwitch enabledSwitch); @Override boolean canHandle(@NotNull ArrayList<Metric> metrics); @Override boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics); }
ZabbixMonitorPublisher extends BufferedHandler<ArrayList<Metric>> { @Override public boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics) { Socket zabbixConnection = null; OutputStream out = null; BufferedReader in = null; try { for (List<Metric> metrics : listOfMetrics) { for (Metric metric : metrics) { zabbixConnection = new Socket(zabbixServer, zabbixPort); StringBuilder message = new StringBuilder(); message.append(MESSAGE_START); message.append(host); message.append(MESSAGE_MIDDLE_LEFT); message.append(metric.getName().replaceAll("\\s", "").replace(":", "")); message.append(MESSAGE_MIDDLE_RIGHT); message.append(metric.getValue()); message.append(MESSAGE_END); String s = message.toString(); byte[] chars = s.getBytes(); int length = chars.length; out = zabbixConnection.getOutputStream(); out.write(new byte[]{ 'Z', 'B', 'X', 'D', '\1', (byte) (length & BITMASK), (byte) ((length >> SHIFT_TWO) & SHIFT_TWO_BITMASK), (byte) ((length >> SHIFT_FOUR) & SHIFT_FOUR_BITMASK), (byte) ((length >> SHIFT_SIX) & SHIFT_SIX_BITMASK), '\0', '\0', '\0', '\0'}); out.write(chars); out.flush(); in = new BufferedReader(new InputStreamReader(zabbixConnection.getInputStream())); LOGGER.debug("Message sent to Zabbix: " + message.toString()); } } } catch (IOException e) { LOGGER.warn("Failed to establish connection to Zabbix", e); return false; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbixConnection != null) { zabbixConnection.close(); } } catch (IOException e) { } } return true; } @Inject  ZabbixMonitorPublisher();  ZabbixMonitorPublisher(OnOffSwitch enabledSwitch); @Override boolean canHandle(@NotNull ArrayList<Metric> metrics); @Override boolean flush(@NotNull Collection<ArrayList<Metric>> listOfMetrics); static final int SHIFT_TWO; static final int SHIFT_FOUR; static final int SHIFT_SIX; static final int BITMASK; static final int SHIFT_TWO_BITMASK; static final int SHIFT_FOUR_BITMASK; static final int SHIFT_SIX_BITMASK; }