CollInformixWithCMD.java 30.7 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 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
package com.sitech.ismp.coll;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
import com.sitech.util.LogUtil;

public class CollInformixWithCMD {

	private Logger logger = Logger.getLogger("COLL");
	private String KBP_ID = "10-11-33";
	private String INFORMIXSERVER;
	private String CMD_DIR;
	private String IP_ADDR;
	private boolean IS_DIR = true;
//	private String OS_Type = "windows";
	private String Version ="9";

	public Vector getConfig(HashMap params) {
		logger.info("into collinformix getConfig");
		initParams(params);
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-10";
		collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER + "-total", 
				"CM-00-03-001-01", this.INFORMIXSERVER);
		if (IS_DIR) {
			try {
				// 采集数据库版本和状态 onstat -f
				String informix_version_cmd = this.CMD_DIR
						+ System.getProperty("file.separator") + "onstat -f";
				Vector version_v = this.execute(informix_version_cmd);
				String vesrion = "unkown";
				String memory = "0";
				if (version_v != null && version_v.size() > 0) {
					System.out.println("size is not 0");
					for (int i = 0; i < version_v.size(); i++) {
						String res = (String) version_v.get(i);
						if (res.indexOf("Version") >= 0) {
							// 
							String[] status = res.split("--");
							if (status != null && status.length > 0) {
								vesrion = status[0];
								memory = status[3];
							}
						}
					}
				} else {
					System.out.println("size is 0");
				}
				// CM-00-03-001-02 数据库版本
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER + "-total", 
						"CM-00-03-001-02", vesrion);
				// CM-00-03-002-01 共享内存大小
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER + "-total", 
						"CM-00-03-002-01", Integer.parseInt((memory.substring(0, memory.length() - 6)).trim()) / 1024 + "");
				// CM-00-03-001-11 数据库监听地址
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER + "-total", 
						"CM-00-03-001-11", this.IP_ADDR);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return collResult.getKPISet();
	}

	public Vector getPerformance(HashMap params) {
		initParams(params);
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-10";
		if (IS_DIR) {
			// 数据库状态 onstat -p
			String state = "DOWN";
			try {
				String informix_version_cmd = this.CMD_DIR
						+ System.getProperty("file.separator") + "onstat -p";
				Vector version_v = this.execute(informix_version_cmd);
				if (version_v != null && version_v.size() > 0) {
					for (int i = 0; i < version_v.size(); i++) {
						String res = (String) version_v.get(i);
						if (res.indexOf("Version") >= 0) {
							String[] status = res.split("--");
							if (status != null && status.length > 0) {
								state = status[1].trim();
							}
						}
					}
				}
			} catch (Exception e) {
				state = e.getMessage();
				LogUtil.error("Exception while getPerformance, db_server = " + this.INFORMIXSERVER, e);
			} finally {
				// FM-00-03-001-09 (informix)数据库状态
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER + "-total", "FM-00-03-001-09", state);
				if (state.equals("DOWN")) {
					return collResult.KPISet;
				}
			}
			
			// 锁数量 onstat -k
			try {
//				String informix_cpu_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -k";
				String locknum_cmd = "sh informixbin/locknum.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
//				logger.info(locknum_cmd);
				Vector locknum_v = this.execute(locknum_cmd);
				String locknum = "0";
				if (locknum_v != null && locknum_v.size() > 0) {
					for (int i = 0; i < locknum_v.size(); i++) {
						String res = (String) locknum_v.get(i);
						if (res != null && res.trim().indexOf("active,") >= 0) {
							res = (String) locknum_v.get(i);
							locknum = split(res, 0);
						}
					}
				}
				// PM-00-03-008-01 锁数量
				config_PRE_UNITID = this.KBP_ID + "-10";
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
						"PM-00-03-008-01", locknum);
			} catch (Exception e) {
				logger.error("coll informix lock number fail, db_server = " + this.INFORMIXSERVER);
				e.printStackTrace();
			}
			
			// 死锁数量 onstat -p
			try {
				String deadlks_cmd = this.CMD_DIR
						+ System.getProperty("file.separator") + "onstat -p";
				Vector deadlks_v = this.execute(deadlks_cmd);
				String deadlks = "0";
				if (deadlks_v != null && deadlks_v.size() > 0) {
					for (int i = 0; i < deadlks_v.size(); i++) {
						String res = (String) deadlks_v.get(i);
						if (res != null && res.trim().indexOf("deadlks") >= 0) {
							res = (String) deadlks_v.get(i + 1);
							deadlks = split(res, 3);
						}
					}
				}
				// PM-00-03-008-01 死锁数量
				config_PRE_UNITID = this.KBP_ID + "-10";
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
						"PM-00-03-008-07", deadlks);
			} catch (Exception e) {
				logger.error("coll informix deadlock number fail, db_server = " + this.INFORMIXSERVER);
				e.printStackTrace();
			}
			
			// session数 onstat -u
			try {
//				String informix_cpu_cmd = this.CMD_DIR
//						+ System.getProperty("file.separator") + "onstat -u";
				String sessionnum_cmd = "sh informixbin/sessionnum.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
				Vector sessionnum_v = this.execute(sessionnum_cmd);
				String sessionnum = "0";
				if (sessionnum_v != null && sessionnum_v.size() > 0) {
					for (int i = 0; i < sessionnum_v.size(); i++) {
						String res = (String) sessionnum_v.get(i);
						if (res != null && res.trim().indexOf("active,") >= 0) {
							res = (String) sessionnum_v.get(i);
							sessionnum = split(res, 0);
						}
					}
				}
				// PM-00-03-009-03 session数
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
						"PM-00-03-009-03", "" + sessionnum);
			} catch (Exception e) {
				logger.error("coll informix session fail,db_server = " + this.INFORMIXSERVER);
				e.printStackTrace();
			}
			
			// 未备份逻辑日志数 onstat -l
			try {
//				String informix_log_cmd = this.CMD_DIR
//						+ System.getProperty("file.separator") + "onstat -l";
				String unbacklognum_cmd = "sh informixbin/unbacklog.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
				Vector unbacklognum_v = this.execute(unbacklognum_cmd);
				int unbackNum = 0;
				if (unbacklognum_v != null && unbacklognum_v.size() > 0) {
					unbackNum = unbacklognum_v.size();
//					for (int i = 0; i < unbacklognum_v.size(); i++) {
//						String res = (String) unbacklognum_v.get(i);
//						if (res != null && res.trim().indexOf("address") >= 0) {
//							for (int j = i + 1; j < unbacklognum_v.size(); j++) {
//								res = (String) unbacklognum_v.get(j);
//								String flag = split(res, 2);
//								if (flag.indexOf("U--") != -1) {
//									unbackNum += 1;
//								}
//							}
//							i = unbacklognum_v.size();
//						}
//					}
				}
				// PM-00-03-005-03 未备份逻辑日志数
				config_PRE_UNITID = this.KBP_ID + "-10";
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
						"PM-00-03-005-03", "" + unbackNum);
			} catch (Exception e) {
				logger.error("coll informix un bakced log number fail, db_server = " + this.INFORMIXSERVER);
				e.printStackTrace();
			}
			
			// 共享内存利用率,读命中率 onstat -p
			try {
				/*
				 * String informix_memory_cmd = this.CMD_DIR +
				 * System.getProperty("file.separator") + INFORMIXSERVER +
				 * "_p.cmd"; if (this.OS_Type.equalsIgnoreCase("unix")) {
				 * informix_memory_cmd = this.CMD_DIR +
				 * System.getProperty("file.separator") + INFORMIXSERVER +
				 * "_p.sh"; }
				 */
				String informix_memory_cmd = this.CMD_DIR
						+ System.getProperty("file.separator") + "onstat -p";
				Vector memory_v = this.execute(informix_memory_cmd);
				if (memory_v != null && memory_v.size() > 0) {
					for (int i = 0; i < memory_v.size(); i++) {
						String res = (String) memory_v.get(i);
						if (res.trim().indexOf("dskreads") >= 0) {
							res = (String) memory_v.get(i + 1);
							if (res != null) {
								String memory_hit_rate = split(res, 3);
								// PM-00-03-001-02 共享内存读命中率
								collResult.addKPI(config_PRE_UNITID + ":"
										+ this.INFORMIXSERVER + "-total", "PM-00-03-001-02", memory_hit_rate);
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("coll informix memory rate  fail,db_server="
						+ this.INFORMIXSERVER);
			}
			
			// 最长事务跨度 
			// 方法一:onstat -x
			// 方法二:执行transmax.sh
			try {
				/*
				 * String informix_trans_cmd=this.CMD_DIR +
				 * System.getProperty("file.separator") + "onstat -x|grep B-|awk
				 * '{printf(\"%s\\t\\t%d\\t\\t%d\\n\",$3,$4,$6-$5)}'";
				 * String informix_trans_cmd=this.CMD_DIR +
				 * System.getProperty("file.separator") + "onstat -x";
				 */
				String informix_sh = "sh informixbin/transmax.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
				logger.info(informix_sh);
				Vector trans_v = this.execute(informix_sh);
				// 根据结果得到最长事务跨度,
				String usrthread = "";
				int max = 0;
				if (trans_v != null && trans_v.size() > 0) {
					for (int i = 0; i < trans_v.size(); i++) {
						String res = (String) trans_v.get(i);
						String threadid = split(res, 0);
						String trans = split(res, 2);
						try {
							if (max <= Integer.parseInt(trans)) {
								max = Integer.parseInt(trans);
								usrthread = threadid;
							}
						} catch (Exception e) {
						}
					}
					logger.info("usrthread :" + usrthread);
					if (usrthread != null && !usrthread.equals("")) {
						/*
						 * String informix_user_cmd = this.CMD_DIR +
						 * System.getProperty("file.separator") + "onstat
						 * -u|grep "+usrthread;
						 */
						String informix_user_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -u";
						logger.info(informix_user_cmd);
						Vector user_v = this.execute(informix_user_cmd);
						String userName = "";
						logger.info("user_v's size is " + user_v.size());
						if (user_v != null && user_v.size() > 0) {
							for (int i = 0; i < user_v.size(); i++) {
								String res = (String) user_v.get(i);
								if (res.indexOf(usrthread) != -1) {
									userName = split(res, 3);
									usrthread = split(res, 2);
									logger.info("userName " + userName);
								}
							}
							if (userName != null && !userName.equals("")) {
								// PM-00-03-008-02 事务最大跨度
								// PM-00-03-008-04 事务最大跨度thread
								// PM-00-03-008-03 事务最大跨度用户
								collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
										"PM-00-03-008-02", "" + max);
								collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
										"PM-00-03-008-04", usrthread);
								collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
										"PM-00-03-008-03", userName);
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
//			// checkpoint持续时间,informix5分钟一次,所以监控时间间隔应该小于5分钟 onstat -m
//			try {
//				String informix_m_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -m";
//				Vector m_v = this.execute(informix_m_cmd);
//				if (m_v != null && m_v.size() > 0) {
//					String time = "";
//					for (int i = 0; i < m_v.size(); i++) {
//						String res = (String) m_v.get(i);
//						if (res.indexOf("Checkpoint Completed:") != -1) {
//							time = split(res, 5);
//						}
//					}
//					// PM-00-03-008-05 checkpoint持续时间
//					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, "PM-00-03-008-05", time);
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
			
			// 虚拟段个数 onstat -g seg
			try {
				/*
				 * String informix_seg_cmd = this.CMD_DIR +
				 * System.getProperty("file.separator") + "onstat -g seg|gerp \"
				 * v \"|wc -l";
				 */
				String informix_seg_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -g seg";
				Vector seg_v = this.execute(informix_seg_cmd);
				if (seg_v != null && seg_v.size() > 0) {
					int size = 0;
					float rate = 0;
					for (int i = 0; i < seg_v.size(); i++) {
						String res = (String) seg_v.get(i);
						if (res.indexOf(" V ") != -1) {
							size += 1;
						}
						if (res.indexOf("Total:") > -1) {
							String totalsize = split(res, 3);
							String user8k = split(res, 6);
							rate = Float.parseFloat(Formater.formatDecimalKpivalue((Float.parseFloat(user8k) * 8) * 100 / Float.parseFloat(totalsize) + ""));
						}
					}
					System.out.println("onstat_g_seg = " + size);
					// PM-00-03-008-06 虚拟段个数
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, "PM-00-03-008-06", "" + size);
					// PM-00-03-009-07 共享内存使用百分比
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, "PM-00-03-009-07", "" + rate);
				} else {
					System.out.println("onstat_g_seg = " + seg_v.size());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// 采集表空间 onstat -d
			try {
				/*
				 * String informix_device_cmd = this.CMD_DIR +
				 * System.getProperty("file.separator") + INFORMIXSERVER +
				 * "_d.cmd"; if (this.OS_Type.equalsIgnoreCase("unix")) {
				 * informix_device_cmd = this.CMD_DIR +
				 * System.getProperty("file.separator") + INFORMIXSERVER +
				 * "_d.sh"; }
				 */
				String informix_device_cmd = this.CMD_DIR 
						+ System.getProperty("file.separator") + "onstat -d";
				Vector Chunks_v = this.execute(informix_device_cmd);
				
				int db_size = 0;							// 得到dbspace数量大小
				int dbspace_end_index = 0;					// dbspace统计到上一行
				int dbspace_begin_index = Chunks_v.size();	// 从下面第2行开始是dbspace数据
				
				if (Chunks_v != null && Chunks_v.size() > 0) {
					for (int i = 0; i < Chunks_v.size(); i++) {
						String res = (String) Chunks_v.get(i);
						if (res.indexOf("active,") != -1) {
							String db_names_num = split(res, 0);
							try {
								db_size = Integer.parseInt(db_names_num);
							} catch (Exception e) {
								e.printStackTrace();
							}
							dbspace_end_index = i;
							break;
						}
						if (res.indexOf("Dbspaces") != -1) {
							dbspace_begin_index = i + 2;
						}
					}
				}
				logger.info("db_size " + db_size);
				String[][] dbnames = null;
//				String[][] dbsize = null;
//				String[][] dbfree = null;
				if (db_size > 0) {
					dbnames = new String[db_size][5];
				}
				int j = 0;
				int dbsize_begin_index = Chunks_v.size();
//				int dbsize_end_index = Chunks_v.size();
				logger.info("dbspace_begin_index " + dbspace_begin_index);
				logger.info("dbspace_end_index " + dbspace_end_index);
				if (Chunks_v != null && Chunks_v.size() > 0) {
//					int dbnames_begin_index = Chunks_v.size();
					for (int i = 0; i < Chunks_v.size(); i++) {
						String res = (String) Chunks_v.get(i);
						// 初始化dbspace name
						if (dbspace_begin_index <= i && i < dbspace_end_index) {
							String dbspace_index = split(res, 1);
							String dbspace_name = "";
							if (this.Version != null && this.Version.equals("9")) {
								dbspace_name = split(res, 8);
							} else if (this.Version != null && this.Version.equals("11")) {
								dbspace_name = split(res, 9);
							} else {
								dbspace_name = split(res, 7);
							}
							dbnames[j][0] = dbspace_index;
							dbnames[j][1] = dbspace_name;
							dbnames[j][2] = "0";
							dbnames[j][3] = "0";
							dbnames[j][4] = "0";
							j += 1;
						}
						// 初始化各个表空间的大小
						logger.info("i " + i);
						if (i > dbspace_end_index) {
							if (res.indexOf("Chunks") != -1) {
								dbsize_begin_index = i + 2;
								logger.info("dbsize_begin_index " + dbsize_begin_index);
							}
							if (i >= dbsize_begin_index) {
								String db_index = split(res, 2);
								String dbchunk_size = split(res, 4);
								String dbchunk_free = split(res, 5);
								String dbchunk_status = split(res, 6);
								this.setChunkSize(dbnames, db_index, dbchunk_size);
								this.setfreeSize(dbnames, db_index, dbchunk_free);
								this.setChunkStatus(dbnames, db_index, dbchunk_status);
							}
						}
					}
				}

				for (int i = 0; i < dbnames.length; i++) {
					config_PRE_UNITID = this.KBP_ID + "-12";
					// CM-00-03-003-01 表空间名称
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + dbnames[i][1], "CM-00-03-003-01", dbnames[i][1]);
					// CM-00-03-003-02 表空间大小
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + dbnames[i][1], "CM-00-03-003-02", dbnames[i][2]);
					// PM-00-03-904-01 表空间剩余大小
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + dbnames[i][1], "PM-00-03-904-01", dbnames[i][3]);
					// FM-00-03-001-02 表空间状态
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + dbnames[i][1], "FM-00-03-001-02", dbnames[i][4]);
					try {
						float useRate = ((Float.parseFloat(dbnames[i][2]) - 
								Float.parseFloat(dbnames[i][3])) / Float.parseFloat(dbnames[i][2]) * 100);
						// PM-00-03-004-01 表空间使用率
						collResult.addKPI(config_PRE_UNITID + ":"
								+ this.INFORMIXSERVER + "-" + dbnames[i][1], "PM-00-03-004-01", Formater.formatDecimalKpivalue(String.valueOf(useRate)));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// 采集数据设备 onstat -D
			try {
				String informix_device_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -D";
				Vector Chunks_v = this.execute(informix_device_cmd);
				// 得到Chunks数量大小
				int dbdevice_size = 0;
				int dbdevice_end_index = 0;
				int dbdevice_begin_index = Chunks_v.size();
				boolean oneActive = true;
				boolean secActive = false;
				if (Chunks_v != null && Chunks_v.size() > 0) {
					for (int i = 0; i < Chunks_v.size(); i++) {
						String res = (String) Chunks_v.get(i);
						if (res.indexOf("active,") != -1) {
							if (oneActive && !secActive) {
								oneActive = false;
								secActive = true;
							} else if (!oneActive && secActive) {
								String dbdevice_num = split(res, 0);
								try {
									dbdevice_size = Integer.parseInt(dbdevice_num);
								} catch (Exception e) {
									e.printStackTrace();
								}
								dbdevice_end_index = i;
							}
						}
						if (res.indexOf("Chunks") != -1) {
							dbdevice_begin_index = i + 2;
						}
					}
				}

				String[][] denames = null;
				if (dbdevice_size > 0) {
					denames = new String[dbdevice_size][4];
				}
				int k = 0;
				int desize_begin_index = Chunks_v.size();
				if (Chunks_v != null && Chunks_v.size() > 0) {
					for (int i = 0; i < Chunks_v.size(); i++) {
						// 初始化datedevice name
						String res = (String) Chunks_v.get(i);
						if (dbdevice_begin_index <= i && i < dbdevice_end_index) {
							String dbdevice_index = split(res, 1);
							String dbdevice_name = "";
							if (this.Version != null && this.Version.equals("9")) {
								dbdevice_name = split(res, 6);
							} else if (this.Version != null && this.Version.equals("11")) {
								dbdevice_name = split(res, 6);
							} else {
								dbdevice_name = split(res, 6);
							}
							String deRd_page = split(res, 4);
							String deWr_page = split(res, 5);
							denames[k][0] = dbdevice_index;
							denames[k][1] = dbdevice_name;
							denames[k][2] = deRd_page;
							denames[k][3] = deWr_page;
							k += 1;
						}
					}
				}

				for (int i = 0; i < denames.length; i++) {
					// 10-11-33-13 数据文件指标
					config_PRE_UNITID = this.KBP_ID + "-13";
					// CM-00-03-006-01 数据设备名
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + denames[i][1], "CM-00-03-006-01", denames[i][1]);
					// PM-00-03-005-01 读操作次数
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + denames[i][1], "PM-00-03-005-01", denames[i][2]);
					// PM-00-03-005-02 写操作次数
					collResult.addKPI(config_PRE_UNITID + ":"
							+ this.INFORMIXSERVER + "-" + denames[i][1], "PM-00-03-005-02", denames[i][3]);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// 最大持锁会话 onstat -u | sort +7 -n | tail -1
			config_PRE_UNITID = this.KBP_ID + "-15";
			try {
				String informix_lock_cmd = "sh informixbin/locksession.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
				Vector lock_v = this.execute(informix_lock_cmd);
				if (lock_v != null && lock_v.size() > 0) {
					String res = (String) lock_v.get(0);
					String sessid = split(res, 2);
					String user = split(res, 3);
					String locks = split(res, 7);
					// 增加采集时间指标
					String pattern = "yyyy-MM-dd HH:mm";
					logger.info("test 10-11-33-15 "+sessid+" --- "+locks+" --- "+user);
					SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"CM-00-01-001-22", dateFormat.format(new java.util.Date()));
					// FM-00-03-001-05 会话ID
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"FM-00-03-001-05", sessid);
					// FM-00-03-001-06 会话用户
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"FM-00-03-001-06", user);
					// FM-00-03-001-07 持锁数
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"FM-00-03-001-07", locks);
				} else {
					System.out.println("informix_lock_cmd = " + lock_v.size());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// 最大内存占用会话 onstat -g ses | sort +6 -n | tail -1
			config_PRE_UNITID = this.KBP_ID + "-16";
			try {
				String mem_sess_cmd = "sh informixbin/memsession.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
				Vector mem_sess_v = this.execute(mem_sess_cmd);
				if (mem_sess_v != null && mem_sess_v.size() > 0) {
					String res = (String) mem_sess_v.get(0);
					String id = split(res, 0);
					String user = split(res, 1);
					String memory = split(res, 6);
					// 增加采集时间指标
					String pattern = "yyyy-MM-dd HH:mm";
					SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"CM-00-01-001-22", dateFormat.format(new java.util.Date()));
					// FM-00-03-001-05 会话ID
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"FM-00-03-001-05", id);
					// FM-00-03-001-06 会话用户
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER,

							"FM-00-03-001-06", user);
					// FM-00-03-001-07 会话占用内存
					collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
							"FM-00-03-001-08", memory);
				} else {
					System.out.println("mem_sess_cmd = " + mem_sess_v.size());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// ready队列 onstat -g rea|grep ready
			try {
				config_PRE_UNITID = this.KBP_ID + "-10";
				String informix_rea_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -g rea";
				Vector rea_v = this.execute(informix_rea_cmd);
				int num = 0;
				if (rea_v != null && rea_v.size() > 0) {
					for (int i = 0; i < rea_v.size(); i++) {
						String res = (String) rea_v.get(i);
						if (res.indexOf("ready") > -1) {
							num++;
						}
					}
				}
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, "PM-00-03-008-08", "" + num);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// 锁等待队列 onstat -u | grep L--
			try {
				config_PRE_UNITID = this.KBP_ID + "-10";
				String lockwaitnum_cmd = "sh informixbin/lockwait.sh " + this.CMD_DIR + " " + this.INFORMIXSERVER;
				Vector lockwaitnum_v = this.execute(lockwaitnum_cmd);
				int lockwaitnum = 0;
				if (lockwaitnum_v != null && lockwaitnum_v.size() > 0) {
					for (int i = 0; i < lockwaitnum_v.size(); i++) {
						String res = (String) lockwaitnum_v.get(i);
						if (res.indexOf("L--") > -1) {
							lockwaitnum++;
						}
					}
				}
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, 
						"PM-00-03-008-09", "" + lockwaitnum);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			// Chunk down onstat -d|grep PD
			try {
				config_PRE_UNITID = this.KBP_ID + "-10";
				String informix_D_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -d";
				Vector D_v = this.execute(informix_D_cmd);
				int num = 0;
				if (D_v != null && D_v.size() > 0) {
					for (int i = 0; i < D_v.size(); i++) {
						String res = (String) D_v.get(i);
						if (res.indexOf("PD") > -1) {
							num++;
						}
					}
				}
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, "PM-00-03-008-10", "" + num);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return collResult.getKPISet();
	}

	/**
	 * 初始化执行命令用到的相关参数
	 * INFORMIXSERVER
	 * CMD_DIR				informix的bin路径
	 * ID_ADDR(IP_ADDR)	informix数据库的IP
	 * VERSION				informix数据库版本 11 or 9
	 * 
	 * @param params
	 */
	public void initParams(HashMap params) {
		this.INFORMIXSERVER = (String) params.get("INFORMIXSERVER");
		this.CMD_DIR = (String) params.get("CMD_DIR");
		this.IP_ADDR = (String) params.get("ID_ADDR");
		this.Version = (String) params.get("VERSION");
		File file = new File(CMD_DIR);
		if (!file.exists() || !file.isDirectory()) {
			logger.error("CMD_DIR is not a directory");
			this.IS_DIR = false;
		}
	}

	/**
	 * 执行下发的查询命令
	 * @throws Exception 
	 */
	private Vector execute(String shellCommand) throws Exception {
		System.out.println("cmd is: " + shellCommand);
		Vector vResult = new Vector();
		try {
			// shellCommand = "sh linux/"+shellCommand;
			Process process = Runtime.getRuntime().exec(shellCommand);
			BufferedReader reader = null;
			reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line = reader.readLine();
			while (line != null) {
				logger.info("line is not null");
				// 执行命令返回的报告可能有多行,逐行存储在Vector中
				vResult.addElement(line);
				line = reader.readLine();
			}
			reader.close();
		} catch (Exception e) {
			logger.error("在本机上可能没有 " + shellCommand + " 命令!");
			throw new Exception(
					"Device not support SNMP or Collect params is wrong or Network is error: ");
		} finally {
			Runtime.getRuntime().freeMemory();
			Runtime.getRuntime().gc();
		}
		return vResult;
	}

	public String split(String str, int pos) {
		StringTokenizer st = new StringTokenizer(str);
		Vector values = new Vector();
		while (st.hasMoreTokens()) {
			values.add(st.nextToken());
		}
		if (pos >= 0 && pos < values.size()) {
			return (String) values.elementAt(pos);
		}
		if(pos==-1){
			return (String)values.elementAt(values.size()-1);
		}
		return "";
	}
	
	public void setChunkSize(String[][] dbnames, String db_index, String chunkSzie) {
		int i = 0;
		for (; i < dbnames.length; i++) {
			String index = dbnames[i][0];
			if (index.equals(db_index)) {
				break;
			}
		}
		if (i < dbnames.length) {
			String size = dbnames[i][2];
			try {
				long size_int = Long.parseLong(size);
				long chunkSzie_int = Long.parseLong(chunkSzie) / 1024;
				long total_size = size_int + chunkSzie_int;
				logger.info("size_int chunkSzie_int total_size " + size_int + " " + chunkSzie_int + " " + total_size);
				dbnames[i][2] = "" + total_size;
			} catch (Exception e) {
				logger.info("get chunksize error");
				e.printStackTrace();
			}
		}
	}
	
	public void setfreeSize(String[][] dbnames, String db_index, String free) {
		int i = 0;
		for (; i < dbnames.length; i++) {
			String index = dbnames[i][0];
			if (index.equals(db_index)) {
				break;
			}
		}
		if (i < dbnames.length) {
			String size = dbnames[i][3];
			try {
				long size_int = Long.parseLong(size);
				long free_int = Long.parseLong(free) / 1024;
				long total_size = size_int + free_int;
				dbnames[i][3] = "" + total_size;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void setChunkStatus(String[][] dbnames, String db_index, String status) {
		int i = 0;
		for (; i < dbnames.length; i++) {
			String index = dbnames[i][0];
			if (index.equals(db_index)) {
				break;
			}
		}
		if (i < dbnames.length) {
			String size = dbnames[i][4];
			try {
//				int size_int = Integer.parseInt(size);
//				int free_int = Integer.parseInt(free);
//				int total_size = size_int + free_int;
				dbnames[i][4] = "" + status;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public Vector getCheckpoint(HashMap params) {
		initParams(params);
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-17";
		// checkpoint持续时间,informix5分钟一次,所以监控时间间隔应该小于5分钟 onstat -m
		try {
			String informix_m_cmd = this.CMD_DIR + System.getProperty("file.separator") + "onstat -m";
			Vector m_v = this.execute(informix_m_cmd);
			if (m_v != null && m_v.size() > 0) {
				String time = "";
				for (int i = 0; i < m_v.size(); i++) {
					String res = (String) m_v.get(i);
					if (this.Version != null && this.Version.equals("11")) {
						if (res.indexOf("Block Time") != -1) {
							time = split(res, 8).substring(0, 5);
						}
					}else{
						if (res.indexOf("Checkpoint Completed:") != -1) {
							time = split(res, 5);
						}
					}
					
				}
				// PM-00-03-008-05 checkpoint持续时间
				collResult.addKPI(config_PRE_UNITID + ":" + this.INFORMIXSERVER, "PM-00-03-008-05", time);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return collResult.getKPISet();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashMap params = new HashMap();
		params.put("INFORMIXSERVER", "ol_ids_1150_1");
		params.put("CMD_DIR", "/informix_cq/bin");
		params.put("ID_ADDR", "127.0.0.1");

		CollInformixWithCMD cmd = new CollInformixWithCMD();
		Vector v=cmd.getConfig(params);
		if(v!=null&&v.size()>0){
			for (int i = 0; i < v.size(); i++) {
				TblATO_KPIDETAIL ato = (TblATO_KPIDETAIL) v.get(i);
				System.out.println(ato.UNIT_ID + " | " + ato.KPI_ID + " | "
						+ ato.KPI_VALUE + " | " + ato.CLL_TIME);
			}
		}
		v = cmd.getPerformance(params);
		if(v!=null&&v.size()>0){
			for (int i = 0; i < v.size(); i++) {
				TblATO_KPIDETAIL ato = (TblATO_KPIDETAIL) v.get(i);
				System.out.println(ato.UNIT_ID + " | " + ato.KPI_ID + " | "
						+ ato.KPI_VALUE + " | " + ato.CLL_TIME);
			}
		}

		String str="16:12:32  Checkpoint Statistics - Avg. Txn Block Time 0.000, # Txns blocked 0, Plog used 649, Llog used 216";
		System.out.println(cmd.split(str, 8).substring(0, 5));
	}

}