Advertisement
Guest User

Untitled

a guest
May 5th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. /**
  2.  * Utility which polls the number of concurrent connections on a Wowza Server
  3.  * It them updates a MySQL Server with this data
  4.  */
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.Statement;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12.  
  13. import javax.management.MBeanServerConnection;
  14. import javax.management.ObjectName;
  15. import javax.management.remote.JMXConnector;
  16. import javax.management.remote.JMXConnectorFactory;
  17. import javax.management.remote.JMXServiceURL;
  18.  
  19. /**
  20.  * @author skaag
  21.  *
  22.  */
  23. public class WowzaMonitor {
  24.  
  25.     /**
  26.      * @param args
  27.      */
  28.     public static void main(String[] args) {
  29.  
  30.         // If this is true, we'll end the program
  31.         boolean tooManyFailures = false;
  32.  
  33.         String dsn = "jdbc:mysql://localhost:3306/indexdb";
  34.  
  35.         Connection con = null;
  36.         Statement stmt = null;
  37.         ResultSet rs = null;
  38.  
  39.         try {
  40.             // Initiate a MySQL Connection
  41.             Class.forName("com.mysql.jdbc.Driver");
  42.             con = DriverManager.getConnection(dsn, "user", "password");
  43.  
  44.             // Obtain the list of servers from the database
  45.             stmt = con.createStatement();
  46.  
  47.             while (!tooManyFailures) {
  48.                 rs = stmt.executeQuery("select * from machines where alive");
  49.                 while (rs.next()) {
  50.                     try {
  51.                         // Obtain data from the result set
  52.                         String serverId = rs.getString("id");
  53.                         String serverName = rs.getString("name");
  54.                         String serverIp = rs.getString("ip");
  55.                         String jmxUser = rs.getString("jmxuser");
  56.                         String jmxPass = rs.getString("jmxpass");
  57.  
  58.                         // Construct the JMX URL Dynamically
  59.                         String jmxUrl = "service:jmx:rmi://" + serverIp
  60.                                 + ":8084/jndi/rmi://" + serverIp
  61.                                 + ":8085/jmxrmi";
  62.  
  63.                         System.out.println("Connecting to " + serverName
  64.                                 + " with username " + jmxUser);
  65.  
  66.                         // create a connection URL
  67.                         JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl);
  68.  
  69.                         // create a environment hash with user names and
  70.                         // passwords
  71.                         Map<String, String[]> env = new HashMap<String, String[]>();
  72.                         String[] creds = { jmxUser, jmxPass };
  73.                         env.put(JMXConnector.CREDENTIALS, creds);
  74.  
  75.                         // connect to the server
  76.                         JMXConnector connector = JMXConnectorFactory.connect(
  77.                                 serviceURL, env);
  78.                         MBeanServerConnection connection = connector
  79.                                 .getMBeanServerConnection();
  80.  
  81.                         // create an ObjectName to the server level Connections
  82.                         // object
  83.                         String connectsName = "WowzaMediaServerPro:name=Connections";
  84.                         String attrName = "current";
  85.                         ObjectName connectsObjName = new ObjectName(
  86.                                 connectsName);
  87.  
  88.                         // Retrieve the concurrent connections
  89.                         Long connectionsObj = (Long) connection.getAttribute(
  90.                                 connectsObjName, attrName);
  91.                         long connections = connectionsObj.longValue();
  92.  
  93.                         // Retrieve the bytes rate
  94.                         connectsName = "WowzaMediaServerPro:name=IOPerformance";
  95.                         attrName = "messagesOutBytesRate";
  96.                         ObjectName outBytesObjName = new ObjectName(
  97.                                 connectsName);
  98.                         Double outBytesRateObj = (Double) connection
  99.                                 .getAttribute(outBytesObjName, attrName);
  100.                         double outBytesRate = outBytesRateObj.doubleValue();
  101.  
  102.                         // Show the user on the console
  103.                         System.out.println("ConnectionCount: " + connections);
  104.                         System.out.println("messagesOutBytesRate: "
  105.                                 + outBytesRate);
  106.  
  107.                         // Update MySQL
  108.                         Statement updateStatement = con.createStatement();
  109.                         updateStatement
  110.                                 .executeUpdate("update machines set currentviewers = '"
  111.                                         + connections
  112.                                         + "', currentbytesoutrate='"
  113.                                         + outBytesRate
  114.                                         + "' where id = "
  115.                                         + serverId);
  116.                     }
  117.  
  118.                     catch (Exception e) {
  119.                         System.out.println("ERROR: " + e.toString());
  120.                     }
  121.                 }
  122.                 // Sleep every cycle
  123.                 Thread.sleep(5000);
  124.             }
  125.  
  126.         } catch (Exception e) {
  127.         }
  128.  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement