Skip to content

Commit

Permalink
Copied to trafshow backend module
Browse files Browse the repository at this point in the history
git-svn-id: http://ns2.paguo.net/svn/repos/trafshow/trunk/backend/snmp@505 11230f00-c60a-0410-812f-9e7afa6d8178
  • Loading branch information
slava committed Nov 15, 2007
1 parent 350f3be commit 0ea92a6
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/net/paguo/statistics/snmp/Main.java
@@ -0,0 +1,45 @@
package net.paguo.statistics.snmp;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.paguo.statistics.snmp.model.HostQuery;
import net.paguo.statistics.snmp.model.HostDefinition;
import net.paguo.statistics.snmp.commands.HostRunner;

import java.sql.SQLException;
import java.util.Set;

/**
* User: slava
* Date: 25.04.2007
* Time: 23:45:11
* Version: $Id$
*/
public class Main {
public static final Log log = LogFactory.getLog(Main.class);

public static void main(String args[]) throws SQLException, InterruptedException {
Thread t = new Thread(new Runner());
t.start();
t.join();
}

public static class Runner implements Runnable{

public void run() {
log.debug("Starting main thread. Getting hosts");
long start = System.currentTimeMillis();
HostQuery q = new HostQuery();
Set<HostDefinition> hostDefinitions = q.getDefinitions();
for (HostDefinition hd : hostDefinitions) {
log.debug(hd.getHostAddress());
Runnable runner = new HostRunner(hd);
Thread t = new Thread(runner);
t.start();
}

long runTime = System.currentTimeMillis() - start;
log.debug("All threads finished. Total time " + runTime + " ms");
}
}
}
136 changes: 136 additions & 0 deletions src/main/java/net/paguo/statistics/snmp/commands/HostRunner.java
@@ -0,0 +1,136 @@
package net.paguo.statistics.snmp.commands;

import net.paguo.statistics.snmp.model.HostDefinition;
import net.paguo.statistics.snmp.model.HostQuery;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
* User: slava
* Date: 26.04.2007
* Time: 1:07:18
* Version: $Id$
*/
public class HostRunner implements Runnable{
private static final Log log = LogFactory.getLog(HostRunner.class);
private HostDefinition definition;
private static final long MAX_TIMEOUT = 2000l;
private static final OID UPTIME_OID = new OID(".1.3.6.1.2.1.1.3.0");
private static final OID INTERFACES_OID = new OID(".1.3.6.1.2.1.2.2.1.2");
private static final OID INCOME_OID = new OID(".1.3.6.1.2.1.2.2.1.10");
private static final OID OUTCOME_OID = new OID(".1.3.6.1.2.1.2.2.1.16");

public HostRunner(HostDefinition definition){
this.definition = definition;
}

public void run() {
try {
long start = System.currentTimeMillis();
doQuery();
log.debug(definition.getHostAddress() + " session time: "
+ (System.currentTimeMillis() - start) + " ms");
} catch (IOException e) {
log.error(e);
}
}

public void doQuery() throws IOException {
HostQuery hq = new HostQuery();
Address snmpAddress = createAddress();
TransportMapping mapping = new DefaultUdpTransportMapping();
CommunityTarget target = createCommunity(snmpAddress);
Snmp snmp = new Snmp(mapping);
snmp.listen();
PDU pdu = uptimePDU();
ResponseEvent evtx = snmp.send(pdu, target);
PDU response = evtx.getResponse();

if (response != null && response.getErrorStatus() == 0){
Vector bindings = response.getVariableBindings();
if (bindings.size() > 0) {
VariableBinding binding = (VariableBinding) bindings.get(0);
Variable variable = binding.getVariable();
long l = variable.toLong();
hq.saveUptime(definition.getHostAddress(), l);
}
}else {
log.error("Time request failed for " + definition.getHostAddress());
return;
}
Map<Long, String> interfaces = getBulk(target, snmp, INTERFACES_OID);
Map<Long, String> inputs = getBulk(target, snmp, INCOME_OID);
Map<Long, String> outputs = getBulk(target, snmp, OUTCOME_OID);
snmp.close();
analyze(hq, interfaces, inputs, outputs);
}

private void analyze(HostQuery hq, Map<Long, String> interfaces, Map<Long, String> inputs, Map<Long, String> outputs) {
hq.saveInterfaces(definition.getHostAddress(), interfaces);
hq.saveInformation(definition.getHostAddress(), interfaces, inputs, outputs);
}

private Map<Long, String> getBulk(Target target, Session snmp, OID base) throws IOException {
Map<Long, String> results = new HashMap<Long, String>();
VariableBinding binding = runGetNext(target, snmp, base);
while(binding != null && child(base, binding.getOid())) {
OID oid = binding.getOid();
Long index = (long) oid.get(oid.size() - 1);
String result = binding.getVariable().toString();
results.put(index, result);
binding = runGetNext(target, snmp, oid);
}
return results;
}

private boolean child(OID base, OID oid) {
return oid.toString().startsWith(base.toString());
}

private VariableBinding runGetNext(Target target, Session snmp, OID oid) throws IOException {
ResponseEvent evtx;
PDU ifaPdu = new PDU();
ifaPdu.add(new VariableBinding(oid));
ifaPdu.setType(PDU.GETNEXT);
evtx = snmp.send(ifaPdu, target);
VariableBinding binding = null;
PDU ifacesResponse = evtx.getResponse();
if (ifacesResponse != null && ifacesResponse.getErrorStatus() == 0){
Vector bindings = ifacesResponse.getVariableBindings();
binding = (VariableBinding) bindings.get(0);
}
return binding;
}

private PDU uptimePDU() {
PDU pdu = new PDU();
pdu.add(new VariableBinding(UPTIME_OID));
pdu.setType(PDU.GET);
return pdu;
}

private Address createAddress() {
return GenericAddress.parse("udp:"
+ definition.getHostAddress() + "/161");
}

private CommunityTarget createCommunity(Address snmpAddress) {
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(definition.getCommunity()));
target.setAddress(snmpAddress);
target.setRetries(2);
target.setTimeout(MAX_TIMEOUT);
target.setVersion(SnmpConstants.version2c);
return target;
}
}
55 changes: 55 additions & 0 deletions src/main/java/net/paguo/statistics/snmp/database/DBProxy.java
@@ -0,0 +1,55 @@
package net.paguo.statistics.snmp.database;

import org.postgresql.ds.PGSimpleDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import net.paguo.statistics.snmp.model.HostDefinition;

/**
* User: slava
* Date: 26.04.2007
* Time: 0:04:16
* Version: $Id$
*/
public class DBProxy {
private DataSource ds;

DBProxy() {
ds = new PGSimpleDataSource();
String dbhost = System.getProperty("dbhost");
String database = System.getProperty("database");
String username = "root";
String password = "test12~";
((PGSimpleDataSource) ds).setUser(username);
((PGSimpleDataSource) ds).setPassword(password);
((PGSimpleDataSource) ds).setServerName(dbhost);
((PGSimpleDataSource) ds).setDatabaseName(database);
}

DBProxy(Properties props) {
ds = new PGSimpleDataSource();
String dbhost = props.getProperty(DBProxyFactory.HOST_KEY);
String database = props.getProperty(DBProxyFactory.DATABASE_KEY);
String username = props.getProperty(DBProxyFactory.USER_KEY);
String password = props.getProperty(DBProxyFactory.PASSWORD_KEY);
((PGSimpleDataSource) ds).setUser(username);
((PGSimpleDataSource) ds).setPassword(password);
((PGSimpleDataSource) ds).setServerName(dbhost);
((PGSimpleDataSource) ds).setDatabaseName(database);
}

public Connection getConnection() throws SQLException {
return ds.getConnection();
}

public void closeConnection(Connection c) throws SQLException {
c.close();
}

public void saveUptime(HostDefinition definition, long l) {
}
}
@@ -0,0 +1,69 @@
package net.paguo.statistics.snmp.database;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* User: slava
* Date: 26.04.2007
* Time: 0:22:17
* Version: $Id$
*/
public class DBProxyFactory {
private static final Log log = LogFactory.getLog(DBProxyFactory.class);
private static DBProxy proxy;
static final String HOST_KEY = "dbhost";
static final String DATABASE_KEY = "database";
static final String USER_KEY = "username";
static final String PASSWORD_KEY = "password";

public static DBProxy getDBProxy() {
if (proxy == null) {
Properties props = loadProperties();
proxy = new DBProxy(props);
}
return proxy;
}

private static Properties loadProperties() {
Properties props = loadDefaultProperties();
String propertyFile = System.getProperty("dbprops");
if (propertyFile != null) {
try {
props = loadFileProperties(propertyFile);
} catch (IOException e) {
log.error(e);
}
}
return props;
}

private static Properties loadFileProperties(String propertyFile) throws IOException {
Properties props;
InputStream is = new FileInputStream(propertyFile);
props = new Properties();
props.load(is);
return props;
}

private static Properties loadDefaultProperties() {
Properties props = new Properties();
props.put(HOST_KEY, "localhost");
props.put(DATABASE_KEY, "traffic");
props.put(USER_KEY, "root");
props.put(PASSWORD_KEY, "test12~");
return props;
}

public static DBProxy getDBProxy(Properties props) {
if (proxy == null) {
proxy = new DBProxy(props);
}
return proxy;
}
}
28 changes: 28 additions & 0 deletions src/main/java/net/paguo/statistics/snmp/model/HostDefinition.java
@@ -0,0 +1,28 @@
package net.paguo.statistics.snmp.model;

/**
* User: slava
* Date: 26.04.2007
* Time: 0:17:29
* Version: $Id$
*/
public class HostDefinition {
private String hostAddress;
private String community;

public String getHostAddress() {
return hostAddress;
}

public void setHostAddress(String hostAddress) {
this.hostAddress = hostAddress;
}

public String getCommunity() {
return community;
}

public void setCommunity(String community) {
this.community = community;
}
}

0 comments on commit 0ea92a6

Please sign in to comment.