CollJuniperWithSNMP.java 29.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
package com.sitech.ismp.coll;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;

import com.sitech.database.dao.TbTmpInterfaceDao;
import com.sitech.database.domain.TbTmpInterface;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.ismp.coll.net.ifOperStatusHelper;
import com.sitech.ismp.coll.net.ifTypeHelper;
import com.sitech.ismp.coll.net.juniper.table.IdsProtectSetTable;
import com.sitech.ismp.coll.net.juniper.table.IdsProtectSetTableRow;
import com.sitech.ismp.coll.net.juniper.table.JuniperConnectionEntry;
import com.sitech.ismp.coll.net.juniper.table.JuniperConnectionWalker;
import com.sitech.ismp.coll.net.juniper.table.ZoneCfgTable;
import com.sitech.ismp.coll.net.juniper.table.ZoneCfgTableRow;
import com.sitech.ismp.coll.net.table.IfTable;
import com.sitech.ismp.coll.net.table.IfTableRow;
import com.sitech.ismp.snmp.IfEntry;
import com.sitech.ismp.snmp.IpAddrEntry;
import com.sitech.util.Formater;

public class CollJuniperWithSNMP {

	private Logger logger = Logger.getLogger("COLL");

	String PRE_UNITID = "10-18-12"; // Juniper

	private String HOST_IP = "";

	private int PORT = 161;

	private String COMMUNITY = "public";

	private String HOST_NAME = "";

	private String NEAT_HOST_NAME = "";

	private Snmp mSnmp = null;

	private CommunityTarget mCommunityTarget = null;

	public CollJuniperWithSNMP(String ip, int port, String community,
			String host_name) {
		this.HOST_IP = ip;
		this.PORT = port;
		this.COMMUNITY = community;
		this.HOST_NAME = host_name;
		this.NEAT_HOST_NAME = com.sitech.util.Formater.neatenunitid(host_name);
	}

	/**
	 * 初始化SNMP target
	 * 
	 */
	public void init() {
		try {
			String addr = "udp:" + this.HOST_IP + "/"
					+ String.valueOf(this.PORT);
			OctetString community = new OctetString(this.COMMUNITY);
			Address address = GenericAddress.parse(addr);
			mCommunityTarget = new CommunityTarget(address, community);
			org.snmp4j.TransportMapping vTransport = new org.snmp4j.transport.DefaultUdpTransportMapping();
			mSnmp = new org.snmp4j.Snmp(vTransport);
			vTransport.listen();
			mCommunityTarget.setCommunity(community);
			mCommunityTarget.setAddress(new UdpAddress(java.net.InetAddress
					.getByName(this.HOST_IP), this.PORT));
			mCommunityTarget.setRetries(2);
			mCommunityTarget.setTimeout(200);
			// mCommunityTarget.setVersion(org.snmp4j.mp.SnmpConstants.version2c);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			logger.error("UnknownHostError", e);
			this.release();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("IOError", e);
			this.release();
		}
	}// end init

	/**
	 * 得到单个oid对应值
	 * 
	 * @param oid
	 * @return
	 */
	public String getKPIInfo(String oid) {
		String result = "";
		this.init();
		PDU pdu = new PDU();
		VariableBinding var = new VariableBinding(new OID(oid),
				new org.snmp4j.smi.OctetString());
		pdu.add(var);
		pdu.setType(PDU.GET);
		try {
			ResponseEvent res = mSnmp.send(pdu, mCommunityTarget);
			PDU result_pdu = res.getResponse();
			if (result_pdu != null) {
				VariableBinding vb = result_pdu.get(0);
				result = vb.getVariable().toString();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.release();
		return result;
	}

	/**
	 * 采集主机状态
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getDeviceState() {
		String oid = ".1.3.6.1.2.1.1.2.0";
		String temp = this.getKPIInfo(oid);
		String status = "";
		if (temp == null || temp.equals("")) {
			status = "DOWN";
		} else {
			status = "UP";
		}

		// FM-01-01-01-01
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "FM-00-01-001-01", status);
	}

	/**
	 * 判断SNMP状态
	 * 
	 * @return true:正常;false:异常
	 */
	public boolean getSNMPState() {
		String value = getKPIInfo(".1.3.6.1.2.1.1.5.0");
		if (value == null) {
			return false;
		}
		return true;
	}

	/**
	 * 释放SNMP连接
	 * 
	 */
	public void release() {
		try {
			if (mSnmp != null) {
				mSnmp.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			mSnmp = null;
		}
	}// end release

	/**
	 * Cpu平均利用率
	 * 
	 * @return
	 */
	public String getCpuAvgPercentage() {
		String oid = "1.3.6.1.4.1.3224.16.1.1.0";
		return this.getKPIInfo(oid);

	}

	/**
	 * Cpu前1分钟利用率
	 * 
	 * @return
	 */
	public String getCpuLast1MinPercentage() {
		String oid = "1.3.6.1.4.1.3224.16.1.2.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * Cpu前5分钟利用率
	 * 
	 * @return
	 */
	public String getCpuLast5MinPercentage() {
		String oid = "1.3.6.1.4.1.3224.16.1.3.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * Cpu前15分钟利用率
	 * 
	 * @return
	 */
	public String getCpuLast15MinPercentage() {
		String oid = "1.3.6.1.4.1.3224.16.1.4.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * 得到分配总内存大小
	 * 
	 * @return
	 */
	public String getMemAllocated() {
		String oid = "1.3.6.1.4.1.3224.16.2.1.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * 得到未使用内存大小
	 * 
	 * @return
	 */
	public String getMemLeft() {
		String oid = "1.3.6.1.4.1.3224.16.2.2.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * 得到内存碎片
	 * 
	 * @return
	 */
	public String getMemFrg() {
		String oid = "1.3.6.1.4.1.3224.16.2.3.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * 正使用的Session数
	 * 
	 * @return
	 */
	public String getSessAllocate() {
		String oid = "1.3.6.1.4.1.3224.16.3.2.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * 允许使用的Session最大数
	 * 
	 * @return
	 */
	public String getSessMaxNum() {
		String oid = "1.3.6.1.4.1.3224.16.3.3.0";
		return this.getKPIInfo(oid);
	}

	/**
	 * 采集端口信息
	 * 
	 * @return
	 */
	public Vector getInterface() {
		IfTable ifTable = new IfTable();
		ifTable.FillTable(HOST_IP, PORT, COMMUNITY);
		Vector result = new Vector();
    	Date newCollTime = new Date();

        try {
        	Vector ifTables = ifTable.getRrows();
        	logger.info("walk interface size : "+ifTables!=null?ifTables.size():0);
        	for(int i = 0 ; i < ifTables.size() ; i++){
        		IfTableRow ifEntry = (IfTableRow)ifTables.elementAt(i);
        		String temp = ifEntry.getIfdesc().toLowerCase();
				if (temp.indexOf("null") >= 0 || temp.indexOf("loopback") >= 0) {
					continue;
				}
        		
        		String UNIT_ID = PRE_UNITID + "-11" + ":" + NEAT_HOST_NAME + "-" + ifEntry.getIfdesc() + "-interface";
        		result.add(createKPI(UNIT_ID, "CM-00-02-002-01", ifEntry.getIfdesc()));
        		result.add(createKPI(UNIT_ID, "CM-00-02-002-02", ifTypeHelper.getTypeDesc(ifEntry.getIftype())));
        		result.add(createKPI(UNIT_ID, "CM-00-02-002-03", ifEntry.getIfspeed()));
        		result.add(createKPI(UNIT_ID, "CM-00-02-002-04", ifEntry.getIfphyaddr()));        		
        		result.add(createKPI(UNIT_ID, "CM-00-02-002-07", ifEntry.getIfindex()));

                
        		String portStatus = "DOWN";
        		String adminStatus = ifEntry.getIfadminstatus();
        		String adminStatuskpi = "DOWN";
        		if("1".equals(adminStatus)){
        			adminStatuskpi = "UP";
        		}else if("3".equals(adminStatus)){
        			adminStatuskpi = "TESTING";
        		}
        		result.add(createKPI(UNIT_ID, "FM-00-02-001-03", adminStatuskpi));
        		String operSatatus = ifEntry.getIfoperstatus();
        		String oprStatuskpi = "DOWN";
        		if("1".equals(operSatatus)){
        			oprStatuskpi = "UP";
        		}else if("3".equals(operSatatus)){
        			oprStatuskpi = "TESTING";
        		}
        		result.add(createKPI(UNIT_ID, "FM-00-02-001-04", oprStatuskpi));
				// 如果用户配置的是up,但实际情况为down,则表示该端口处于down的状态;
				if("1".equals(adminStatus)){
					if("2".equals(operSatatus)){
						portStatus = "DOWN";
					}else 
					if("1".equals(operSatatus)){
						portStatus = "UP";
					}else 
					if("3".equals(operSatatus)){
						portStatus = "TESTING";
					}
				}else if("2".equals(adminStatus)){
					portStatus = "DOWN";
				}else if("3".equals(adminStatus)){
					portStatus = "TESTING";
				}
        		result.add(createKPI(UNIT_ID, "FM-00-02-001-02", portStatus));
        		String ifSpeed = ifEntry.getIfspeed();
        		IfEntry entry = new IfEntry();
        		entry.setIfIndex(ifEntry.getIfindex());
        		entry.setIfInOctets(ifEntry.getIfinoctets());
        		entry.setIfInUcastPkts(ifEntry.getIfinucastpkts());
        		entry.setIfInNUcastPkts(ifEntry.getIfinnucastpkts());
        		entry.setIfInDiscards(ifEntry.getIfindiscards());
        		entry.setIfInErrors(ifEntry.getIfinerrors());
        		entry.setIfOutOctets(ifEntry.getIfoutoctets());
        		entry.setIfOutUcastPkts(ifEntry.getIfoutucastpkts());
        		entry.setIfOutNUcastPkts(ifEntry.getIfoutnucastpkts());
        		entry.setIfOutDiscards(ifEntry.getIfoutdiscards());
        		entry.setIfOutErrors(ifEntry.getIfouterrors());
        		
        		TbTmpInterface newIf = new TbTmpInterface(entry, this.HOST_IP, newCollTime);       		
        		TbTmpInterface oldIf = queryTbTmpInterface(this.HOST_IP, entry.getIfIndex());
        		if(oldIf == null){
        			// 首次采集
        			addTbTmpInterface(newIf);
        			continue;
        		}else{
        			updateTbTmpInterface(newIf);
        		}
        		
        		Date oldCollTime = oldIf.getCOLL_TIME();
        		

        		String ifInOctets = entry.getIfInOctets();
        		String ifInUcastPkts = entry.getIfInUcastPkts();
        		String ifInNUcastPkts = entry.getIfInNUcastPkts();
        		String ifInDiscards = entry.getIfInDiscards();
        		String ifInError = entry.getIfInErrors();
        		String ifOutOctets = entry.getIfOutOctets();
        		String ifOutUcastPkts = entry.getIfOutUcastPkts();
        		String ifOutNUcastPkts = entry.getIfOutNUcastPkts();
        		String ifOutDiscards = entry.getIfOutDiscards();
        		String ifOutError = entry.getIfOutErrors();
        		
        		String oldInOctets = oldIf.getIF_IN_OCTETS();
        		String oldInUcastPkts = oldIf.getIF_IN_UCASTPKTS();
        		String oldInNUcastPkts = oldIf.getIF_IN_NUCASTPKTS();
        		String oldInDiscards = oldIf.getIF_IN_DISCARDS();
        		String oldInError = oldIf.getIF_IN_ERRORS();
        		String oldOutOctets = oldIf.getIF_OUT_OCTETS();
        		String oldOutUcastPkts = oldIf.getIF_OUT_UCASTPKTS();
        		String oldOutNUcastPkts = oldIf.getIF_OUT_NUCASTPKTS();
        		String oldOutDiscards = oldIf.getIF_OUT_DISCARDS();
        		String oldOutError = oldIf.getIF_OUT_ERRORS();       		
        		
        		
        		// 计算差值
        		String difInOctets = Formater.counter32_sub(ifInOctets, oldInOctets);
        		String difOutOctets = Formater.counter32_sub(ifOutOctets, oldOutOctets);                
       		 	String difInError = Formater.counter32_sub(ifInError, oldInError);
       		 	String difOutError = Formater.counter32_sub(ifOutError, oldOutError);    		 
       		 	String difInDiscards = Formater.counter32_sub(ifInDiscards, oldInDiscards);
       		 	String difOutDiscards = Formater.counter32_sub(ifOutDiscards, oldOutDiscards); 
       		 	String difInUCastPkts = Formater.counter32_sub(ifInUcastPkts, oldInUcastPkts);
       		 	String difOutUcastPkts = Formater.counter32_sub(ifOutUcastPkts, oldOutUcastPkts); 
       		 	String difInNUcastPkts = Formater.counter32_sub(ifInNUcastPkts, oldInNUcastPkts);
       		 	String difOutNUCastPkts = Formater.counter32_sub(ifOutNUcastPkts, oldOutNUcastPkts);
                
                // T单位:秒
                String T = String.valueOf(Formater.parseExpression("("
                        + String.valueOf(newCollTime.getTime()) + "-"
                        + String.valueOf(oldCollTime.getTime()) + ")/1000"));
                
                // PM-00-02-001-01 LAN(半双工)端口利用率
                // (△ifInOctes+△ifOUtOetets)/(T*ifSpeed*8)*100%
                String ifUseRate = Formater.double2String(Formater
                        .parseExpression("("+ difInOctets + "+" + difOutOctets +")" + "/(" + T + "*" + ifSpeed
                        + ")*8*100"));
                result.add(createKPI(UNIT_ID, "PM-00-02-001-01", ifUseRate));
                
                // PM-00-02-001-02 WAN(全双工)端口流入利用率 △ifInOctes/(T*ifSpeed)*8*100%   //modified by lj
                String ifInUseRate = Formater.double2String(Formater
                        .parseExpression(difInOctets + "/(" + T + "*" + ifSpeed
                        + ")*8*100"));
                result.add(createKPI(UNIT_ID, "PM-00-02-001-02", ifInUseRate));
                // PM-00-02-001-03 WAN(全双工)端口流出利用率
                // △ifOUtOetets/(T*ifSpeed*8)*100%
                String ifOutUseRate = Formater.double2String(Formater
                        .parseExpression(difOutOctets + "/(" + T + "*"
                        + ifSpeed + ")*8*100"));  //modified by lj
                result.add(createKPI(UNIT_ID, "PM-00-02-001-03", ifOutUseRate));

                //PM-00-02-001-24 端口流入速率 △ifInOctes/T*8*100%
                String ifInOctetsRate = Formater.double2String(Formater
                        .parseExpression(difInOctets + "/" + T + "*8"));
                result.add(createKPI(UNIT_ID, "PM-00-02-001-24", ifInOctetsRate));

                //PM-00-02-001-25 端口流出速率 △ifOutOctes/T*8*100%
                String ifOutOctetsRate = Formater.double2String(Formater
                        .parseExpression(difOutOctets + "/" + T + "*8"));
                result.add(createKPI(UNIT_ID, "PM-00-02-001-25", ifOutOctetsRate));

                // PM-00-02-001-04 端口误码率
                // (△ifInErrors+△ifOutErrors)/(△ifInUcastPkts+△ifInNUcastPkts+△ifOutUcastPkts+△ifOutNUcastPkts)*100%
                String ifErrorRate = Formater.double2String(Formater
                        .parseExpression("(" + difInError + "+" + difOutError
                        + ")/(" + difInUCastPkts + "+"
                        + difInNUcastPkts + "+" + difOutUcastPkts + "+"
                        + difOutNUCastPkts + ")*100")); //modified by lj
                result.add(createKPI(UNIT_ID, "PM-00-02-001-04", ifErrorRate));
                // PM-00-02-001-05 端口丢包率
                // (△ifInDiscards+△ifOutDiscards)/(△ifInUcastPkts+△ifInNUcastPkts+△ifInUcastPkts+△ifInNUcastPkts)*100%
                String ifDiscardsRate = Formater.double2String(Formater
                        .parseExpression("(" + difInDiscards + "+"
                        + difOutDiscards + ")/(" + difInUCastPkts + "+"
                        + difInNUcastPkts + "+" + difOutUcastPkts + "+"
                        + difOutNUCastPkts + ")*100"));  //modified by lj
                result.add(createKPI(UNIT_ID, "PM-00-02-001-05", ifDiscardsRate));
                
                // PM-00-02-001-11 端口流入错包数	△ifInErrors
                result.add(createKPI(UNIT_ID, "PM-00-02-001-11", difInError));
                // PM-00-02-001-12 端口流出错包数	△ifOutErrors
                result.add(createKPI(UNIT_ID, "PM-00-02-001-12", difOutError));
                // PM-00-02-001-13 端口流入丢包数	△ifInDiscards
                result.add(createKPI(UNIT_ID, "PM-00-02-001-13", difInDiscards));
                // PM-00-02-001-14 端口流出丢包数	△ifOutDiscards
                result.add(createKPI(UNIT_ID, "PM-00-02-001-14", difOutDiscards));
        	}
        }catch (Exception e) {
			logger.error("Exception while getInterface", e);
		}
		
		return result;
	}

	/**
	 * 得到主机名称
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPIHostName() {
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-00-01-001-01", this.HOST_NAME);
	}
	
	/**
	 * 采集设备类型
	 * @return
	 */
	public Vector getDevConfig() {
		Vector kpiVector = new Vector();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		kpiVector.add(createKPI(UNIT_ID, "CM-00-02-001-01", "Firewall"));
		kpiVector.add(createKPI(UNIT_ID, "CM-00-02-001-02", "Juniper"));
		// 设备名称
		String devName = this.getKPIInfo("1.3.6.1.2.1.1.5.0");
		kpiVector.add(createKPI(UNIT_ID, "CM-00-02-001-04", devName));
		kpiVector.add(createKPI(UNIT_ID, "CM-00-02-001-05", HOST_IP));
		//启动时长:.1.3.6.1.2.1.1.3.0
		String upTime = this.getKPIInfo("1.3.6.1.2.1.1.3.0");
		kpiVector.add(createKPI(UNIT_ID, "CM-00-02-001-07", upTime));
		//端口数量 .1.3.6.1.2.1.2.1.0
		String portNum = this.getKPIInfo("1.3.6.1.2.1.2.1.0");
		kpiVector.add(createKPI(UNIT_ID, "CM-00-02-001-06", portNum));
		return kpiVector;
	}
	
	/**
	 * 采集IpAddr
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getIpAddr() {
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-00-02-001-05", this.HOST_IP);
	}

	/**
	 * CPU使用率
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPIPercentageCpu() {
		String cpuPercentage = this.getCpuLast1MinPercentage();
		String UNIT_ID = PRE_UNITID + "-11" + ":" + NEAT_HOST_NAME + "-cpu";
		return this.createKPI(UNIT_ID, "PM-00-02-001-06", String
				.valueOf(cpuPercentage));

	}

	/**
	 * 内存大小
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPIPhiMemory() {
		String memAll = this.getMemAllocated();
		int int_memAll = Integer.parseInt(memAll) / 1024 / 1024 / 8;
		String UNIT_ID = PRE_UNITID + "-12" + ":" + NEAT_HOST_NAME + "-memory";
		return this.createKPI(UNIT_ID, "CM-00-01-001-07", String
				.valueOf(int_memAll));
	}

	/**
	 * 内存使用率
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPIMemoryUseRate() {

		String totalMemory = this.getMemAllocated();
		String freeMemory = this.getMemLeft();

		if (totalMemory == null || freeMemory == null) {
			return null;
		}

		int total = 0;
		int free = 0;
		int used = 0;
		double usedRate = 0;

		try {
			total = Integer.parseInt(totalMemory);
			free = Integer.parseInt(freeMemory);
			used = total - free;
			usedRate = used * 100.0 / total;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String strUsedRate = com.sitech.util.Formater
				.formatDecimalKpivalue(String.valueOf(usedRate));
		String memory_PRE_UNITID = PRE_UNITID + "-12";
		String UNIT_ID = memory_PRE_UNITID + ":" + NEAT_HOST_NAME + "-memory";
		return this.createKPI(UNIT_ID, "PM-00-02-001-07", strUsedRate);

	}

	/**
	 * 内存碎片占用率
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPIMemoryFragmentRate() {

		String totalMemory = this.getMemAllocated();
		String frgMemory = this.getMemFrg();

		if (totalMemory == null || frgMemory == null) {
			return null;
		}

		int total = 0;
		int frg = 0;
		double frgRate = 0;

		try {
			total = Integer.parseInt(totalMemory);
			frg = Integer.parseInt(frgMemory);
			frgRate = frg * 100.0 / total;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String strUsedRate = com.sitech.util.Formater
				.formatDecimalKpivalue(String.valueOf(frgRate));
		String UNIT_ID = PRE_UNITID + "-12" + ":" + NEAT_HOST_NAME + "-memory";
		return this.createKPI(UNIT_ID, "PM-00-01-002-09", strUsedRate);
	}

	/**
	 * 允许最大Session数
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPISessionMax() {
		String sessionMax = this.getSessMaxNum();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-00-07-001-10", sessionMax);
	}

	/**
	 * 当前Session数
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPISessionCurr() {
		String sessionCurr = this.getSessAllocate();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "PM-00-07-001-10", sessionCurr);
	}

	/**
	 * Session占用率
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getKPISessionRate() {
		String sessionCurr = this.getSessAllocate();
		String sessionMax = this.getSessMaxNum();
		int max = 0;
		int curr = 0;
		double Rate = 0;
		try {
			max = Integer.parseInt(sessionMax);
			curr = Integer.parseInt(sessionCurr);
			Rate = curr * 100.0 / max;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sessionRate = com.sitech.util.Formater
				.formatDecimalKpivalue(String.valueOf(Rate));
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "PM-00-07-001-11", sessionRate);
	}

	private String serviceStatus(String num) {
		String result = "Unknown";
		if (num.trim().equals("1")) {
			result = "On";
		} else if (num.trim().equals("0")) {
			result = "Off";
		}
		return result;
	}

	/**
	 * 入侵检测开启状态
	 * 
	 * @return
	 */
	public Vector getKPIIdsStatus() {

		Vector kpiVector = new Vector();
		// 10-15-11-17 security
		IdsProtectSetTable IdsTable = new IdsProtectSetTable();
		IdsTable.FillTable(HOST_IP, PORT, COMMUNITY);
		Vector idsVector = IdsTable.getRrows();

		ZoneCfgTable zoneTable = new ZoneCfgTable();
		zoneTable.FillTable(HOST_IP, PORT, COMMUNITY);
		Vector zoneVector = zoneTable.getRrows();

		for (int i = 0; i < idsVector.size(); i++) {
			IdsProtectSetTableRow idsRow = (IdsProtectSetTableRow) idsVector
					.get(i);
			String zoneIndex = idsRow.getZoneIndex();
			String zoneName = "";
			String podStatue = idsRow.getDetectPOD();
			String tearDropStatus = idsRow.getTearDrop();
			String winNukeStatus = idsRow.getDetectWinNuke();
			String filterISRStatus = idsRow.getFilterIpSrcRoute();
			String protScanStatus = idsRow.getPortScan();
			String addrSweepStatus = idsRow.getAddrSweep();
			String landStatus = idsRow.getLandAttack();
			String ipSpllfingStatus = idsRow.getIPSpoofing();
			String synStatus = idsRow.getSYNAttack();
			String icmpFloodStatus = idsRow.getICMPFlood();
			String udpFloodStatus = idsRow.getUDPFlood();
			for (int j = 0; j < zoneVector.size(); j++) {
				ZoneCfgTableRow zoneRow = (ZoneCfgTableRow) zoneVector.get(j);
				String zoneIndex1 = zoneRow.getZoneIndex();
				if (zoneIndex.equals(zoneIndex1)) {
					zoneName = zoneRow.getZoneName();
				}
			}
			// CM-00-07-001-12 区域名称
			// CM-00-07-001-13 入侵检测服务名称
			// FM-00-07-001-14 入侵检测服务状态
			String UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-POD";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector
					.add(createKPI(UNIT_ID, "CM-00-07-001-13", "Ping of Death"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(podStatue)));
			// ///////////////////////////////////////////

			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-TD";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "Tear Drop"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(tearDropStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-WN";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "Win Nuke"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(winNukeStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-FISRO";
			kpiVector.add(createKPI(UNIT_ID, "PM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "PM-00-07-001-13",
					"Filter IP Source Route Option"));
			kpiVector.add(createKPI(UNIT_ID, "PM-00-07-001-14", this
					.serviceStatus(filterISRStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-PSD";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13",
					"Port Scan Death"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(protScanStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-AS";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector
					.add(createKPI(UNIT_ID, "CM-00-07-001-13", "Address Sweep"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(addrSweepStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-LA";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "Land Attack"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(landStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-IS";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "IP Spoofing"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(ipSpllfingStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-SYN";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "SYN Attack"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(synStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-ICMP";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "ICMP Flood"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(icmpFloodStatus)));

			// ///////////////////////////////////////////
			UNIT_ID = PRE_UNITID + "-17" + ":" + NEAT_HOST_NAME + "-"
					+ zoneIndex + "-security-UDP";
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-12", zoneName));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-07-001-13", "UDP Flood"));
			kpiVector.add(createKPI(UNIT_ID, "FM-00-07-001-14", this
					.serviceStatus(udpFloodStatus)));

		}
		return kpiVector;
	}

	// ////////////////////////////////////////////////////////////////////////////

	/**
	 * 生成TblATO_KPIDETAIL
	 * 
	 * @param KBP_ID
	 * @param KPI_ID
	 * @param KPI_VALUE
	 * @return
	 */
	private TblATO_KPIDETAIL createKPI(String KBP_ID, String KPI_ID,
			String KPI_VALUE) {
		TblATO_KPIDETAIL tblato_kpidetail = new TblATO_KPIDETAIL();
		tblato_kpidetail.UNIT_ID = KBP_ID;
		tblato_kpidetail.KPI_ID = KPI_ID;
		tblato_kpidetail.KPI_VALUE = KPI_VALUE;
		tblato_kpidetail.CLL_TIME = new Date();
		return tblato_kpidetail;
	}
	
	public Vector getConnection(){
		Vector kpiVector = new Vector();
		JuniperConnectionWalker walker = new JuniperConnectionWalker();
		walker.walker(HOST_IP, PORT, COMMUNITY);
		Vector SupplyVector = walker.getEntry();
		for (int i = 0; i < SupplyVector.size(); i++) {
			JuniperConnectionEntry row = (JuniperConnectionEntry) SupplyVector.get(i);
            String UNIT_ID = PRE_UNITID + "-16" + ":" + NEAT_HOST_NAME+"-"+row.getNsPlyMonId();
            //CM-01-02-03-15    10-13   策略ID   
			kpiVector.add(createKPI(UNIT_ID, "CM-01-02-03-15", row.getNsPlyMonId()));
			//PM-00-02-800-09   10-13    每分通过包数
			kpiVector.add(createKPI(UNIT_ID, "PM-00-02-800-09", row.getNsPlyMonPackPerMin()));
			//PM-00-02-800-10    10-13    每分通过字节数
			kpiVector.add(createKPI(UNIT_ID, "PM-00-02-800-10", row.getsPlyMonBytePerMin()));
			//PM-00-02-800-11   10-13  每分通过的会话数
			kpiVector.add(createKPI(UNIT_ID, "PM-00-02-800-11", row.getNsPlyMonSessionPerMin()));
			//PM-00-02-800-12  10-13 通过的总会话数
			kpiVector.add(createKPI(UNIT_ID, "PM-00-02-800-12", row.getNsPlyMonTotalSession()));
		}
		return kpiVector;
	}
	
	private TbTmpInterface queryTbTmpInterface(String ipAddr, String ifIndex) {
		TbTmpInterfaceDao dao = new TbTmpInterfaceDao();
		return dao.queryTbTmpInterface(ipAddr, ifIndex);
	}
	 private void updateTbTmpInterface(TbTmpInterface newIf) {
	    TbTmpInterfaceDao dao = new TbTmpInterfaceDao();
	    dao.deleteTbTmpInterface(newIf);
	    dao.addTbTmpInterface(newIf);		
	}

	private void addTbTmpInterface(TbTmpInterface newIf) {
		TbTmpInterfaceDao dao = new TbTmpInterfaceDao();
		dao.addTbTmpInterface(newIf);		
	}

	public static void main(String[] args) {
		String ip = "130.30.15.6";
		int port = 161;
		String community = "ahnms2008";
		String host_name = "juniper";
		CollJuniperWithSNMP juniper = new CollJuniperWithSNMP(ip, port,
				community, host_name);
		String str = juniper.getSessAllocate();
		System.out.println(str);

		// 1.3.6.1.4.1.3224.16.3.2.0
	}
}