diff --git a/healthdisplay/.idea/libraries/Maven__log4j_log4j_1_2_9.xml b/healthdisplay/.idea/libraries/Maven__log4j_log4j_1_2_9.xml new file mode 100644 index 0000000..37a2238 --- /dev/null +++ b/healthdisplay/.idea/libraries/Maven__log4j_log4j_1_2_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/healthdisplay/.idea/libraries/Maven__org_snmp4j_snmp4j_1_9_1f.xml b/healthdisplay/.idea/libraries/Maven__org_snmp4j_snmp4j_1_9_1f.xml new file mode 100644 index 0000000..31252ff --- /dev/null +++ b/healthdisplay/.idea/libraries/Maven__org_snmp4j_snmp4j_1_9_1f.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/healthdisplay/healthdisplay.iml b/healthdisplay/healthdisplay.iml index 8a76843..d987de2 100644 --- a/healthdisplay/healthdisplay.iml +++ b/healthdisplay/healthdisplay.iml @@ -17,6 +17,8 @@ + + diff --git a/healthdisplay/pom.xml b/healthdisplay/pom.xml index 30de015..53cfc74 100644 --- a/healthdisplay/pom.xml +++ b/healthdisplay/pom.xml @@ -25,6 +25,11 @@ forms_rt 7.0.3 + + org.snmp4j + snmp4j + 1.9.1f + diff --git a/healthdisplay/src/main/java/de/psychose/ActorDisplay.form b/healthdisplay/src/main/java/de/psychose/ActorDisplay.form index 1884b4e..d72dc0a 100644 --- a/healthdisplay/src/main/java/de/psychose/ActorDisplay.form +++ b/healthdisplay/src/main/java/de/psychose/ActorDisplay.form @@ -140,16 +140,6 @@ - - - - - - - - - - @@ -170,6 +160,16 @@ + + + + + + + + + + diff --git a/healthdisplay/src/main/java/de/psychose/ActorDisplay.java b/healthdisplay/src/main/java/de/psychose/ActorDisplay.java index 542cd83..abef1e0 100644 --- a/healthdisplay/src/main/java/de/psychose/ActorDisplay.java +++ b/healthdisplay/src/main/java/de/psychose/ActorDisplay.java @@ -3,7 +3,6 @@ package de.psychose; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; -import java.util.Random; /** * @author: lucas @@ -73,14 +72,10 @@ public class ActorDisplay { } public ActorDisplay() { - final Random r = new Random(); - final Timer timer = new Timer(100, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { -// actorPanel.setBackground(new Color(r.nextInt(), false)); - if (++counterTemperature > timeout) { lblTemperature.setForeground(offColor); } else { @@ -113,6 +108,7 @@ public class ActorDisplay { if (++counterHeartbeat > timeout) { lblHeartbeat.setForeground(offColor); + lblHeartbeat.setText("no data"); } else { lblHeartbeat.setForeground(onColor); } @@ -122,7 +118,6 @@ public class ActorDisplay { } else { lblBreath.setForeground(onColor); } - } }); timer.setRepeats(true); diff --git a/healthdisplay/src/main/java/de/psychose/MainForm.java b/healthdisplay/src/main/java/de/psychose/MainForm.java index d4cd4b1..45fe384 100644 --- a/healthdisplay/src/main/java/de/psychose/MainForm.java +++ b/healthdisplay/src/main/java/de/psychose/MainForm.java @@ -17,6 +17,7 @@ import java.util.Date; */ public class MainForm { private ChaOSCclient osCclient; + private SnmpStatClient snmpStatClient; private JPanel mainPanel; private ActorDisplay actor1; @@ -27,8 +28,12 @@ public class MainForm { private int totalMessageCount = 0; private int messagesTempCounter = 0; - public MainForm(ChaOSCclient chaOSCclient) { - osCclient = chaOSCclient; + private long totalTraffic = 0; + private long lastTraffic = 0; + + public MainForm(final ChaOSCclient chaOSCclient, final SnmpStatClient snmpStatClient) { + this.osCclient = chaOSCclient; + this.snmpStatClient = snmpStatClient; addActor("merle", "Proband 1", actor1); addActor("uwe", "Proband 2", actor2); @@ -46,6 +51,18 @@ public class MainForm { }); timer.setRepeats(true); timer.start(); + + final Timer snmpTimer = new Timer(5000, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + totalTraffic = snmpStatClient.getTrafficSum(); + statDisplay.setTotalTraffic(String.valueOf(totalTraffic / 1024)); + statDisplay.setBandwidth(String.valueOf((totalTraffic - lastTraffic) / 1024 / 5)); + lastTraffic = totalTraffic; + } + }); + snmpTimer.setRepeats(true); + snmpTimer.start(); } @@ -56,7 +73,7 @@ public class MainForm { public void acceptMessage(Date time, OSCMessage message) { if (message.getArguments().length == 3) { totalMessageCount++; - actorDisplay.setHeartbeat(message.getArguments()[0].toString()); + actorDisplay.setHeartbeat(message.getArguments()[0].toString().equals("0") ? "Systole" : "Diastole"); actorDisplay.setPulse(message.getArguments()[1].toString()); actorDisplay.setOxy(message.getArguments()[2].toString()); } @@ -111,7 +128,8 @@ public class MainForm { try { final ChaOSCclient chaOSCclient = new ChaOSCclient(host, port); - final MainForm mainForm = new MainForm(chaOSCclient); + final SnmpStatClient snmp = new SnmpStatClient("switch/161"); + final MainForm mainForm = new MainForm(chaOSCclient, snmp); final JFrame frame = new JFrame("MainForm"); frame.setContentPane(mainForm.mainPanel); frame.setResizable(false); diff --git a/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java b/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java new file mode 100644 index 0000000..5825031 --- /dev/null +++ b/healthdisplay/src/main/java/de/psychose/SnmpStatClient.java @@ -0,0 +1,81 @@ +package de.psychose; + +import org.snmp4j.CommunityTarget; +import org.snmp4j.Snmp; +import org.snmp4j.TransportMapping; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.smi.OID; +import org.snmp4j.smi.OctetString; +import org.snmp4j.smi.UdpAddress; +import org.snmp4j.smi.VariableBinding; +import org.snmp4j.transport.DefaultUdpTransportMapping; +import org.snmp4j.util.DefaultPDUFactory; +import org.snmp4j.util.TreeEvent; +import org.snmp4j.util.TreeUtils; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +/** + * @author: lucas + * @date: 18.04.14 12:10 + */ +public class SnmpStatClient { + public static final String OID_COUNTER = "1.3.6.1.2.1.2.2.1.10"; + private final String host; + private HashMap lastPorts = new HashMap<>(); + private HashMap sumPorts = new HashMap<>(); + + private CommunityTarget getCommunityTarget() { + CommunityTarget communityTarget = new CommunityTarget(); + communityTarget.setCommunity(new OctetString("public")); + communityTarget.setVersion(SnmpConstants.version2c); + communityTarget.setAddress(new UdpAddress(host)); + communityTarget.setTimeout(500); + return communityTarget; + } + + public SnmpStatClient(String host) { + this.host = host; + } + + public long getTrafficSum() { + + long sum = 0; + + try { + final TransportMapping transportMapping = new DefaultUdpTransportMapping(); + transportMapping.listen(); + + final Snmp snmp = new Snmp(transportMapping); + final TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory()); + final List treeEventList = treeUtils.getSubtree(getCommunityTarget(), new OID(OID_COUNTER)); + + for (TreeEvent treeEvent : treeEventList) { + if (treeEvent.getStatus() == TreeEvent.STATUS_OK) { + for (VariableBinding binding : treeEvent.getVariableBindings()) { + int oid = binding.getOid().last(); + long value = binding.getVariable().toLong(); + long lastValue = 0; + if(lastPorts.containsKey(oid)) lastValue = lastPorts.get(oid); + long diff = value - lastValue; + + if(diff > 0) { + sumPorts.put(oid, lastValue + diff); + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + for(long port: sumPorts.values()) { + sum+=port; + } + + return sum; + } + +} diff --git a/healthdisplay/src/main/java/de/psychose/StatsDisplay.form b/healthdisplay/src/main/java/de/psychose/StatsDisplay.form index 0dd0183..0ea389a 100644 --- a/healthdisplay/src/main/java/de/psychose/StatsDisplay.form +++ b/healthdisplay/src/main/java/de/psychose/StatsDisplay.form @@ -1,6 +1,6 @@
- + @@ -24,7 +24,7 @@ - + @@ -47,16 +47,6 @@ - - - - - - - - - - @@ -77,7 +67,27 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -87,6 +97,16 @@ + + + + + + + + + + diff --git a/healthdisplay/src/main/java/de/psychose/StatsDisplay.java b/healthdisplay/src/main/java/de/psychose/StatsDisplay.java index 4989777..7b3c76c 100644 --- a/healthdisplay/src/main/java/de/psychose/StatsDisplay.java +++ b/healthdisplay/src/main/java/de/psychose/StatsDisplay.java @@ -11,6 +11,8 @@ public class StatsDisplay { private JLabel lblMessageCount; private JLabel lblMessagesPerSec; private JPanel statPanel; + private JLabel lblTraffic; + private JLabel lblBandwidth; public void setMessageCount(String count) { lblMessageCount.setText(count); @@ -20,4 +22,12 @@ public class StatsDisplay { lblMessagesPerSec.setText(messagesPerSec); } + public void setTotalTraffic(String totalTraffic) { + lblTraffic.setText(totalTraffic); + } + + public void setBandwidth(String bandwidth) { + lblBandwidth.setText(bandwidth); + } + } diff --git a/healthdisplay/src/main/java/de/psychose/Test.java b/healthdisplay/src/main/java/de/psychose/Test.java index deb48b6..6a1e54c 100644 --- a/healthdisplay/src/main/java/de/psychose/Test.java +++ b/healthdisplay/src/main/java/de/psychose/Test.java @@ -27,6 +27,13 @@ public class Test { public static void main(String[] args) throws Exception { + SnmpStatClient snmpStatClient = new SnmpStatClient("switch/161"); + + System.out.println(snmpStatClient.getTrafficSum() / 1024 / 1024 + "MB"); + + + if(true) return; + Enumeration nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) { displayInterfaceInformation(netint);