www.pudn.com > netsnmp.rar > netsnmp.java
import com.adventnet.snmp.beans.*;
import java.io.*;
import java.sql.*;
import java.awt.Toolkit;
public class netsnmp {
public static void main(String args[])throws Exception
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=snmpdb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
SnmpTarget target = new SnmpTarget();
target.setTargetHost(args[0]);
target.setCommunity(args[1]);
String resultin,resultout,lresultin,lresultout,Alert;
lresultin=null;lresultout=null;Alert=null;
Double dertrx,dertro,ro,rx,lyv,lyuce,yuce,avin,avout,lavin,wucha;
lyuce=null;lavin=null;
int ifspeed =100000000;
for(;;){
try{
Connection con =DriverManager.getConnection("jdbc;odbc;snmpdb","sa","");
target.setObjectID(".1.3.6.1.2.1.2.2.1.10.16");
resultin = target.snmpGet();
if (resultin == null)
System.err.println("Failed;"+target.getErrorString());
else
{Statement stmt =con.createStatement();
target.setObjectID(".1.3.6.1.2.1.2.2.1.16.16");
resultout=target.snmpGet();
if (resultout==null)
System.err.println("Failed;"+target.getErrorString());
else
{rx=Double.parseDouble(resultin);ro=Double.parseDouble(resultout);
if (rx0.8)
for(int i=0;i<5;i++)
{Toolkit.getDefaultToolkit().beep();Thread.sleep(500);}
if (wucha>10)
for(int i=0;i<10;i++)
{Toolkit.getDefaultToolkit().beep();System.out.println("网络流量变化较大");
Alert="***";Thread.sleep(500);}
stmt.executeUpdate("insert into flow(inoct,outoct,avin,avout,lyv,wucha,Alert)values('"+resultin+"','"+resultout+"','"+avin+"','"+avout+"','"+lyv+"','"+wucha+"','"+Alert+"');");
con.close();
Thread.sleep(300000);}}
}catch(Exception e)
{System.out.println(e);}
}
}
}