Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Utility which polls the number of concurrent connections on a Wowza Server
- * It them updates a MySQL Server with this data
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.HashMap;
- import java.util.Map;
- import javax.management.MBeanServerConnection;
- import javax.management.ObjectName;
- import javax.management.remote.JMXConnector;
- import javax.management.remote.JMXConnectorFactory;
- import javax.management.remote.JMXServiceURL;
- /**
- * @author skaag
- *
- */
- public class WowzaMonitor {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // If this is true, we'll end the program
- boolean tooManyFailures = false;
- String dsn = "jdbc:mysql://localhost:3306/indexdb";
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Initiate a MySQL Connection
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(dsn, "user", "password");
- // Obtain the list of servers from the database
- stmt = con.createStatement();
- while (!tooManyFailures) {
- rs = stmt.executeQuery("select * from machines where alive");
- while (rs.next()) {
- try {
- // Obtain data from the result set
- String serverId = rs.getString("id");
- String serverName = rs.getString("name");
- String serverIp = rs.getString("ip");
- String jmxUser = rs.getString("jmxuser");
- String jmxPass = rs.getString("jmxpass");
- // Construct the JMX URL Dynamically
- String jmxUrl = "service:jmx:rmi://" + serverIp
- + ":8084/jndi/rmi://" + serverIp
- + ":8085/jmxrmi";
- System.out.println("Connecting to " + serverName
- + " with username " + jmxUser);
- // create a connection URL
- JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl);
- // create a environment hash with user names and
- // passwords
- Map<String, String[]> env = new HashMap<String, String[]>();
- String[] creds = { jmxUser, jmxPass };
- env.put(JMXConnector.CREDENTIALS, creds);
- // connect to the server
- JMXConnector connector = JMXConnectorFactory.connect(
- serviceURL, env);
- MBeanServerConnection connection = connector
- .getMBeanServerConnection();
- // create an ObjectName to the server level Connections
- // object
- String connectsName = "WowzaMediaServerPro:name=Connections";
- String attrName = "current";
- ObjectName connectsObjName = new ObjectName(
- connectsName);
- // Retrieve the concurrent connections
- Long connectionsObj = (Long) connection.getAttribute(
- connectsObjName, attrName);
- long connections = connectionsObj.longValue();
- // Retrieve the bytes rate
- connectsName = "WowzaMediaServerPro:name=IOPerformance";
- attrName = "messagesOutBytesRate";
- ObjectName outBytesObjName = new ObjectName(
- connectsName);
- Double outBytesRateObj = (Double) connection
- .getAttribute(outBytesObjName, attrName);
- double outBytesRate = outBytesRateObj.doubleValue();
- // Show the user on the console
- System.out.println("ConnectionCount: " + connections);
- System.out.println("messagesOutBytesRate: "
- + outBytesRate);
- // Update MySQL
- Statement updateStatement = con.createStatement();
- updateStatement
- .executeUpdate("update machines set currentviewers = '"
- + connections
- + "', currentbytesoutrate='"
- + outBytesRate
- + "' where id = "
- + serverId);
- }
- catch (Exception e) {
- System.out.println("ERROR: " + e.toString());
- }
- }
- // Sleep every cycle
- Thread.sleep(5000);
- }
- } catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement