snmp4j introduction and Application

Introduction to SNMP

SNMP refers to the Simple Network Management Protocol.It belongs to the application layer protocol of the TCP/IP five-layer protocol.It provides a simple and convenient mode to manage elements in a network.The elements here are the managed objects, either a hardware in the Internet, such as a network card, or a collection of configuration parameters for some hardware and software.Because the SNMP protocol is simple and reliable, it is welcomed by many manufacturers and has become the most widely used network management protocol.

The SNMP protocol consists of two main parts: SNMP management station and SNMP proxy.The SNMP management station is a central node responsible for collecting and processing information for each SNMP element, which is then fed back to the network administrator. The SNMP agent runs on each managed network node, is responsible for counting the information of the node, and is responsible for interacting with the SNMP management site, receiving and executing commands from the management site, and uploading various local websites.Network information.

There is loose coupling between the SNMP management station and the SNMP proxy.Communication between them is done through UDP protocol.Generally, the SNMP management station sends various commands to the SNMP agent through the UDP protocol. When the SNMP agent receives the command, it returns the parameters required by the SNMP management station.However, when the SNMP agent detects network element anomalies, it can also actively send messages to the SNMP management station to notify the current anomaly.

The SNMP protocol was published in 1988.So far, we have experienced three versions: V1, V2 and V3.Among them, V1 has been abandoned and V2c, although not able to become an official standard, has been accepted by many manufacturers. V3 is currently the official standard for the Internet.Compared with V1, V2 and V3 are more suitable for large-scale network management and have made greater security improvements.

2. Win7 Opens SNMP Service 

Monitoring a Windows host through SNMP requires installing the Simple Network Management Protocol (SNMP) Windows component on the server being monitored. Take Windows 7 as an example:
First, find the Uninstaller in the Control Panel;

Click "Turn Windows on or off" in the pop-up window;

Check the Simple Network Management Protocol (SNMP) item in the pop-up window, click OK, and complete the installation as prompted.

 

After the installation of the SNMP service is complete, right-click Computer and select Manage

 


In the pop-up "Computer Management" window, find "Service" in the left navigation bar and "SNMP Service" item on the right;

 

Double-click the "SNMP Service" option and switch to the "Security" tab in the pop-up window, such as adding the "Accepted Community Name" and receiving SNMP packets from those hosts in the image above.

 

"Accepted Community Name" is any character you define. Receiving SNMP packets from those hosts is defined as your Nagios server.The SNMP service of the Windows host on the monitored side is configured here.

2. Linux

1. Check if there is any SNMP service, if not, install it first

service snmpd status #Check to see if snmp is installed

yum install-y net-snmp* #If not installed, install

2. Edit Snmp's configuration file first to set up a secure authentication method

vi /etc/snmp/snmpd.conf

3. Set up, switch SNMP

service snmpd start

service snmpd stop

4. Set SNMP start-up auto-start

chkconfig snmpd on

chkconfig snmpd off

5. Enhanced SNMP Security

/etc/sysconfig/iptables

iptables -A INPUT  -p udp -s 192.168.2.18 --dport 161 -j ACCEPT

service iptables save

The purpose of this is to allow only the specified monitoring server (IP) to establish a connection with your SNMP service.

 

3. Examples of snmp4j

Preparations: snmp4j's jar package can be found on its official website http://www.snmp4j.org/ Download

1. How to get CPU utilization

//Get cpu usage
public static void collectCPU() {
TransportMapping transport = null ;
Snmp snmp = null ;
CommunityTarget target;
String[] oids = {"1.3.6.1.2.1.25.3.3.1.2"};
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);//Create snmp
snmp.listen();//Listen for messages
target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setRetries(2);
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));
target.setTimeout(8000);
target.setVersion(SnmpConstants.version2c);
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {
@Override
public PDU createPDU(Target arg0) {
PDU request = new PDU();
request.setType(PDU.GET);
return request;
}
});
OID[] columns = new OID[oids.length];
for (int i = 0; i < oids.length; i++)
columns[i] = new OID(oids[i]);
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
if(list.size()==1 && list.get(0).getColumns()==null){
System.out.println(" null");
}else{
int percentage = 0;
for(TableEvent event : list){
VariableBinding[] values = event.getColumns();
if(values != null) 
percentage += Integer.parseInt(values[0].getVariable().toString());
}
System.out.println("CPU Utilization rate is:"+percentage/list.size()+"%");
}
} catch(Exception e){
e.printStackTrace();
}finally{
try {
if(transport!=null)
transport.close();
if(snmp!=null)
snmp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

2. Method of obtaining memory information

//Get memory-related information
public static void collectMemory() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String[] oids = {"1.3.6.1.2.1.25.2.3.1.2",  //Type Storage unit type
     "1.3.6.1.2.1.25.2.3.1.3",  //descr  
     "1.3.6.1.2.1.25.2.3.1.4",  //Unit storage unit size
     "1.3.6.1.2.1.25.2.3.1.5",  //size, total storage unit number
     "1.3.6.1.2.1.25.2.3.1.6"}; //Use the number of storage units;
String PHYSICAL_MEMORY_OID = "1.3.6.1.2.1.25.2.1.2";//Physical Storage
String VIRTUAL_MEMORY_OID = "1.3.6.1.2.1.25.2.1.3"; //Virtual Storage
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);//Create snmp
snmp.listen();//Listen for messages
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
OID[] columns = new OID[oids.length];  
for (int i = 0; i < oids.length; i++)  
columns[i] = new OID(oids[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
int unit = Integer.parseInt(values[2].getVariable().toString());//Unit storage unit size
int totalSize = Integer.parseInt(values[3].getVariable().toString());//size, total storage unit number
int usedSize = Integer.parseInt(values[4].getVariable().toString());//used. Use the number of storage units
String oid = values[0].getVariable().toString();  
if (PHYSICAL_MEMORY_OID.equals(oid)){  
System.out.println("PHYSICAL_MEMORY----->Physical memory size:"+(long)totalSize * unit/(1024*1024*1024)+"G   Memory usage:"+(long)usedSize*100/totalSize+"%");  
}else if (VIRTUAL_MEMORY_OID.equals(oid)) {  
System.out.println("VIRTUAL_MEMORY----->Virtual memory size:"+(long)totalSize * unit/(1024*1024*1024)+"G   Memory usage:"+(long)usedSize*100/totalSize+"%");  
}  
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  

3. How to get disk information

//Get disk-related information
public static void collectDisk() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String DISK_OID = "1.3.6.1.2.1.25.2.1.4";  
String[] oids = {"1.3.6.1.2.1.25.2.3.1.2",  //Type Storage unit type
     "1.3.6.1.2.1.25.2.3.1.3",  //descr  
     "1.3.6.1.2.1.25.2.3.1.4",  //Unit storage unit size
     "1.3.6.1.2.1.25.2.3.1.5",  //size, total storage unit number
     "1.3.6.1.2.1.25.2.3.1.6"}; //Use the number of storage units;
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);//Create snmp
snmp.listen();//Listen for messages
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
OID[] columns = new OID[oids.length];  
for (int i = 0; i < oids.length; i++)  
columns[i] = new OID(oids[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null ||!DISK_OID.equals(values[0].getVariable().toString()))   
continue;  
int unit = Integer.parseInt(values[2].getVariable().toString());//Unit storage unit size
int totalSize = Integer.parseInt(values[3].getVariable().toString());//size, total storage unit number
int usedSize = Integer.parseInt(values[4].getVariable().toString());//used. Use the number of storage units
System.out.println(getChinese(values[1].getVariable().toString())+"   Disk size:"+(long)totalSize*unit/(1024*1024*1024)+"G   Disk usage:"+(long)usedSize*100/totalSize+"%");  
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  

4. Method of getting server process information

//Server Process Collection Information
public static void collectProcess() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String[] oids =  
{"1.3.6.1.2.1.25.4.2.1.1",  //index  
            "1.3.6.1.2.1.25.4.2.1.2",  //name  
            "1.3.6.1.2.1.25.4.2.1.4",  //run path  
     "1.3.6.1.2.1.25.4.2.1.6",  //type  
     "1.3.6.1.2.1.25.5.1.1.1",  //cpu  
     "1.3.6.1.2.1.25.5.1.1.2"}; //memory    
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);  
snmp.listen();  
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
OID[] columns = new OID[oids.length];  
for (int i = 0; i < oids.length; i++)  
columns[i] = new OID(oids[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
String name = values[1].getVariable().toString();//name  
String cpu = values[4].getVariable().toString();//cpu  
String memory = values[5].getVariable().toString();//memory  
String path = values[2].getVariable().toString();//path  
System.out.println("name--->"+name+"  cpu--->"+cpu+"  memory--->"+memory+"  path--->"+path);  
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  

5. Getting Server System Services

//Server System Services Collection
public static void collectService() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String[] oids =  
{"1.3.6.1.4.1.77.1.2.3.1.1"};  
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);  
snmp.listen();  
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
OID[] columns = new OID[oids.length];  
for (int i = 0; i < oids.length; i++)  
columns[i] = new OID(oids[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
String name = values[0].getVariable().toString();//name  
System.out.println("Name--->"+getChinese(name));//Chinese random code, need to be converted to utf-8 encoding
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  

6. Method of getting interface information

//Server Interface Collection
public static void collectInterface() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String[] IF_OIDS =   
    {"1.3.6.1.2.1.2.2.1.1",  //Index   
     "1.3.6.1.2.1.2.2.1.2",  //descr  
     "1.3.6.1.2.1.2.2.1.3",  //type  
     "1.3.6.1.2.1.2.2.1.5",  //speed  
     "1.3.6.1.2.1.2.2.1.6",  //mac  
     "1.3.6.1.2.1.2.2.1.7",  //adminStatus  
     "1.3.6.1.2.1.2.2.1.8",  //operStatus     
        
     "1.3.6.1.2.1.2.2.1.10", //inOctets  
     "1.3.6.1.2.1.2.2.1.16", //outOctets  
     "1.3.6.1.2.1.2.2.1.14", //inError  
     "1.3.6.1.2.1.2.2.1.20", //outError  
     "1.3.6.1.2.1.2.2.1.13", //inDiscard  
     "1.3.6.1.2.1.2.2.1.19", //outDiscard  
     "1.3.6.1.2.1.2.2.1.11", //inUcastPkts  
     "1.3.6.1.2.1.2.2.1.17", //outUcastPkts  
     "1.3.6.1.2.1.2.2.1.12", //inNUcastPkts       
     "1.3.6.1.2.1.2.2.1.18"};//outNUcastPkts  
String[] IP_OIDS =     
    {"1.3.6.1.2.1.4.20.1.1", //ipAdEntAddr  
     "1.3.6.1.2.1.4.20.1.2", //ipAdEntIfIndex  
     "1.3.6.1.2.1.4.20.1.3"};//ipAdEntNetMask   
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);  
snmp.listen();  
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
OID[] columns = new OID[IF_OIDS.length];  
for (int i = 0; i < IF_OIDS.length; i++)  
columns[i] = new OID(IF_OIDS[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
System.out.println("interface ---Index: "+values[0].getVariable().toString()+"  descr: "+getChinese(values[1].getVariable().toString())+"  type: "+values[2].getVariable().toString()+" speed: "+values[3].getVariable().toString()+" mac: "+getChinese(values[4].getVariable().toString())+" adminStatus: "+values[5].getVariable().toString()+"  operStatus: "+values[6].getVariable().toString());  
}  
}  
//Get ip
OID[] ipcolumns = new OID[IP_OIDS.length];  
for (int i = 0; i < IP_OIDS.length; i++)  
ipcolumns[i] = new OID(IP_OIDS[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> iplist = tableUtils.getTable(target, ipcolumns, null, null);  
if(iplist.size()==1 && iplist.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : iplist){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
System.out.println(" IP--->ipAdEntAddr:"+values[0].getVariable().toString()+"   ipAdEntIfIndex:"+values[1].getVariable().toString()+"   ipAdEntNetMask:"+values[2].getVariable().toString());  
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  

7. Method of getting port information

//Server Port Collection
public static void collectPort() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String[] TCP_CONN = {"1.3.6.1.2.1.6.13.1.1", //status  
        "1.3.6.1.2.1.6.13.1.3"}; //port  
   
String[] UDP_CONN = {"1.3.6.1.2.1.7.5.1.2"};  
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);  
snmp.listen();  
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
//Get TCP Port
OID[] columns = new OID[TCP_CONN.length];  
for (int i = 0; i < TCP_CONN.length; i++)  
columns[i] = new OID(TCP_CONN[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
int status = Integer.parseInt(values[0].getVariable().toString());  
System.out.println("status--->"+status+"   TCP_port--->"+values[1].getVariable().toString());  
}  
}  
//Get udp port
OID[] udpcolumns = new OID[UDP_CONN.length];  
for (int i = 0; i < UDP_CONN.length; i++)  
udpcolumns[i] = new OID(UDP_CONN[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> udplist = tableUtils.getTable(target, udpcolumns, null, null);  
if(udplist.size()==1 && udplist.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : udplist){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
String name = values[0].getVariable().toString();//name  
System.out.println("UDP_port--->"+name);  
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
} 

8. Ways to get installed software information

//Server Installation Software Collection
public static void collectSoft() {  
TransportMapping transport = null ;  
Snmp snmp = null ;  
CommunityTarget target;  
String[] oids =  
{   "1.3.6.1.2.1.25.6.3.1.2",  //software  
                    "1.3.6.1.2.1.25.6.3.1.4",  //type  
    "1.3.6.1.2.1.25.6.3.1.5"}; //install date  
try {  
transport = new DefaultUdpTransportMapping();  
snmp = new Snmp(transport);  
snmp.listen();  
target = new CommunityTarget();  
target.setCommunity(new OctetString("public"));  
target.setRetries(2);  
target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));  
target.setTimeout(8000);  
target.setVersion(SnmpConstants.version2c);  
TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {  
@Override  
public PDU createPDU(Target arg0) {  
PDU request = new PDU();  
request.setType(PDU.GET);  
return request;  
}  
});  
OID[] columns = new OID[oids.length];  
for (int i = 0; i < oids.length; i++)  
columns[i] = new OID(oids[i]);  
@SuppressWarnings("unchecked")  
List<TableEvent> list = tableUtils.getTable(target, columns, null, null);  
if(list.size()==1 && list.get(0).getColumns()==null){  
System.out.println(" null");  
}else{  
for(TableEvent event : list){  
VariableBinding[] values = event.getColumns();  
if(values == null) continue;  
String software = values[0].getVariable().toString();//software  
String type = values[1].getVariable().toString();//type  
String date = values[2].getVariable().toString();//date  
System.out.println("Software Name--->"+getChinese(software)+"  type--->"+type+"  Installation Time--->"+hexToDateTime(date.replace("'", "")));  
}  
}  
} catch(Exception e){  
e.printStackTrace();  
}finally{  
try {  
if(transport!=null)  
transport.close();  
if(snmp!=null)  
snmp.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
}  
}  

9. Other methods

/** 
 * Get the Chinese name of the disk
 * Solve Chinese Scrambling in snmp4j
 */  
public static String getChinese(String octetString){  
if(octetString == null || "".equals(octetString)   
       || "null".equalsIgnoreCase(octetString)) return "";  
try{  
String[] temps = octetString.split(":");  
if(temps.length < 2)  
return octetString;  
byte[] bs = new byte[temps.length];  
for(int i=0;i<temps.length;i++)  
bs[i] = (byte)Integer.parseInt(temps[i],16);  
    return new String(bs,"GB2312");  
}catch(Exception e){  
return null;  
}  
}  
/** 
 * Convert hexadecimal time to a standard time format
 */  
private static String hexToDateTime(String hexString) {  
if(hexString == null || "".equals(hexString))  
return "";  
String dateTime = "";  
try {  
byte[] values = OctetString.fromHexString(hexString).getValue();  
int year, month, day, hour, minute;  
   
year = values[0] * 256 + 256 + values[1];  
month = values[2];  
day = values[3];  
hour = values[4];  
minute = values[5];  
   
char format_str[] = new char[22];  
int index = 3;  
int temp = year;  
for (; index >= 0; index--) {  
format_str[index] = (char) (48 + (temp - temp / 10 * 10));  
temp /= 10;  
}  
format_str[4] = '-';  
index = 6;  
temp = month;  
for (; index >= 5; index--) {  
format_str[index] = (char) (48 + (temp - temp / 10 * 10));  
temp /= 10;  
}  
format_str[7] = '-';  
index = 9;  
temp = day;  
for (; index >= 8; index--) {  
format_str[index] = (char) (48 + (temp - temp / 10 * 10));  
temp /= 10;  
}  
format_str[10] = ' ';  
index = 12;  
temp = hour;  
for (; index >= 11; index--) {  
format_str[index] = (char) (48 + (temp - temp / 10 * 10));  
temp /= 10;  
}  
format_str[13] = ':';  
index = 15;  
temp = minute;  
for (; index >= 14; index--) {  
format_str[index] = (char) (48 + (temp - temp / 10 * 10));  
temp /= 10;  
}  
dateTime = new String(format_str,0,format_str.length).substring(0, 16);  
} catch (Exception e) {  
//LogFactory.getLog(getClass()).error(e);  
}  
return dateTime;  
} 

 

123 original articles were published, 469 won acceptance, 330,000 visits+
Private letter follow

Tags: network Windows iptables Mac

Posted on Thu, 13 Feb 2020 19:32:20 -0800 by DJH