예제 #1
0
func Probe(ipaddr string, serverid string, ch chan int) {
	count := 0
	log.Println("Ip:", ipaddr, " Serverid:", serverid, " Connect mysql")
	//	db, err := sql.Open("mysql", "kingyp_ustc:123456@tcp(172.19.123.121:3306)/srvadmin?charset=utf8")
	//db, err := sql.Open("mysql", "root:123456@tcp(172.19.123.121:3306)/srvadmin?charset=utf8")
	db, err := sql.Open("mysql", "root:mysql@/srvadmin?charset=utf8")
	defer db.Close()
	checkErr(err)

	g.Default.Target = ipaddr
	err = g.Default.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	//defer uage is not clear....
	//defer g.Default.Conn.Close()
	monitoritem_names := []string{"ssCpuIdle",
		"memTotalReal",
		"memAvailReal",
		"diskTotal",
		"diskUsed",
		"memBuffer",
		"memCached",
		"currSessions"}

	oids := []string{"1.3.6.1.4.1.2021.11.11.0",
		"1.3.6.1.4.1.2021.4.5.0",
		"1.3.6.1.4.1.2021.4.6.0",
		"1.3.6.1.4.1.2021.9.1.6.1",
		"1.3.6.1.4.1.2021.9.1.8.1",
		"1.3.6.1.4.1.2021.4.14.0",
		"1.3.6.1.4.1.2021.4.15.0",
		"1.3.6.1.2.1.25.1.5.0"}

	netcardids := []string{}
	value, ok := ServeridNetcardid[serverid]
	if ok != true {
		log.Fatalf("Get netcardid from serverid error: %s", serverid)
	}
	netcardids = strings.Split(value, "#")

	var m_oidnetcardid map[string]string
	m_oidnetcardid = make(map[string]string, 3*len(netcardids)+1)

	for _, netcardid := range netcardids {
		index, ok := NetcardIndex[netcardid]
		if ok != true {
			log.Fatalf("Get index from netcarid error: %s", netcardid)
			continue
		}
		t_strs := []string{"1.3.6.1.2.1.2.2.1.5.",
			"1.3.6.1.2.1.2.2.1.10.",
			"1.3.6.1.2.1.2.2.1.16."}
		for i, strs := range t_strs {
			var a_strs string
			var a_name string
			a_strs = strs + strconv.Itoa(index)
			switch i {
			case 0:
				a_name = netcardid + "#" + "ifSpeed"
			case 1:
				a_name = netcardid + "#" + "ifInOctets"
			case 2:
				a_name = netcardid + "#" + "ifOutOctets"
			default:
				log.Println("Gen monitor itemname error: %s, %s", netcardid, a_strs)
			}
			oids = append(oids, a_strs)
			monitoritem_names = append(monitoritem_names, a_name)
			m_oidnetcardid[a_strs] = netcardid
		}
	}

	//	fmt.Println(m_oidnetcardid)

	//	//	tmpChan := make(chan int)
	//	//	go func() {
	//	//		fmt.Println("-------%d", len(numbers))
	//	//		tmpnum := <-tmpChan
	//	//	}()
	//	//	tmpnum := 1
	//	//	tmpChan <- tmpnum

	numbers := []float64{}
	defer g.Default.Conn.Close()
	result, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Printf("Serverid:%s, IPAddr:%s Get() err: %v", serverid, ipaddr, err2)
		ch <- count
		os.Exit(0)
	}

	for _, variable := range result.Variables {
		switch variable.Type {
		case g.OctetString:
		default:
			tmp := (g.ToBigInt(variable.Value)).Int64()
			//fmt.Println(reflect.TypeOf(tmp))
			//fmt.Printf("number: %d\n", tmp)
			numbers = append(numbers, float64(tmp))
		}
	}
	//	fmt.Println(monitoritem_names)
	//	fmt.Println(numbers)
	//	fmt.Println(oids)
	//process the databases

	t1 := time.Now().Format("2006-01-02 15:04:05")
	for index, value := range numbers {
		var monitoritem string
		monitoritem = monitoritem_names[index]
		err = db.QueryRow("SELECT server_id FROM t_monitor_record WHERE server_id=? and monitor_item=?", serverid, monitoritem).Scan(&serverid)
		if index == 8 {
			//insert cpuUsage,memUsage,diskUsage
			var cpuUsage float64
			var memUsage float64
			var diskUsage float64
			cpuUsage = (100 - numbers[0]) / 100
			memUsage = (numbers[1] - numbers[2] - numbers[5] - numbers[6]) / numbers[1]
			diskUsage = numbers[4] / numbers[3]
			s_cpuUsage := fmt.Sprintf("%.5f", cpuUsage)
			s_memUsage := fmt.Sprintf("%.5f", memUsage)
			s_diskUsage := fmt.Sprintf("%.5f", diskUsage)
			cpuUsage, _ = strconv.ParseFloat(s_cpuUsage, 64)
			memUsage, _ = strconv.ParseFloat(s_memUsage, 64)
			diskUsage, _ = strconv.ParseFloat(s_diskUsage, 64)
			stmt0, err := db.Prepare("INSERT t_monitor_record SET server_id=?,monitor_item=?," +
				"sample_value=?,data_type=?,sample_time=?")
			checkErr(err)

			_, err = stmt0.Exec(serverid, "cpuUsage", cpuUsage, "double", t1)
			checkErr(err)
			_, err = stmt0.Exec(serverid, "memUsage", memUsage, "double", t1)
			checkErr(err)
			_, err = stmt0.Exec(serverid, "diskUsage", diskUsage, "double", t1)
			checkErr(err)
		}
		switch {
		case err == sql.ErrNoRows:
			log.Printf("No user with that ServerID:%s Monitoritem:%s.\n", serverid, monitoritem)
			//insert: 2 steps , first handle some items : second handle sql
			if ok := strings.Contains(monitoritem, "ifSpeed"); ok == true {
				var t_netcardname string
				var t_netcardid string
				t_netcardid = strings.Split(monitoritem, "#")[0]
				t_netcardname, ok := NetcardidName[t_netcardid]
				if ok != true {
					log.Fatalln("Probe: get the vport netcardname from netcardid %s error", t_netcardid)
				}
				if ok := strings.Contains(t_netcardname, "vport"); ok == true && value == 0 {
					value = 1000000000
				}
			}

			stmt, err := db.Prepare("INSERT t_monitor_record SET server_id=?,monitor_item=?," +
				"sample_value=?,data_type=?,sample_time=?")
			checkErr(err)

			_, err = stmt.Exec(serverid, monitoritem, value, "double", t1)
			checkErr(err)

			//			id, err := res.LastInsertId()
			//			checkErr(err)

			//			fmt.Println(id)
		case err != nil:
			log.Fatal(err)
		default:
			//			fmt.Printf("-------")
			if monitoritem == "memTotalReal" || monitoritem == "diskTotal" {
				continue
			}
			if ok := strings.Contains(monitoritem, "ifSpeed"); ok == true {
				continue
			}
			//insert another
			//fmt.Println(value, serverid, monitoritem)
			/*
				stmt, err := db.Prepare("UPDATE t_monitor_record SET sample_value=?,sample_time=? WHERE server_id=? and monitor_item=?")
				checkErr(err)

				res, err := stmt.Exec(value, t1, serverid, monitoritem)
				checkErr(err)

				affect, err := res.RowsAffected()
				checkErr(err)
			*/
			stmt, err := db.Prepare("INSERT t_monitor_record SET server_id=?,monitor_item=?," +
				"sample_value=?,data_type=?,sample_time=?")
			checkErr(err)

			res, err := stmt.Exec(serverid, monitoritem, value, "double", t1)
			checkErr(err)

			_, err = res.LastInsertId()
			checkErr(err)
		}

	}

	//insert cpuUsage, memUsage, diskUsage

	time.Sleep(time.Duration(tick) * time.Second)

	//	//insert data

	//	//update data

	//	//qurey data
	//	rows, err := db.Query("SELECT * FROM userinfo")
	//	checkErr(err)

	//	for rows.Next() {
	//		var uid int
	//		var username string
	//		var department string
	//		var created string
	//		err = rows.Scan(&uid, &username, &department, &created)
	//		checkErr(err)
	//		fmt.Println(uid)
	//		fmt.Println(username)
	//		fmt.Println(department)
	//		fmt.Println(created)
	//	}

	//delete data

	//	stmt, err = db.Prepare("delete from userinfo where uid=?")
	//	checkErr(err)

	//	res, err = stmt.Exec(id)
	//	checkErr(err)

	//	affect, err = res.RowsAffected()
	//	checkErr(err)

	//	fmt.Println(affect)
	ch <- count
}
예제 #2
0
//insert or update t_server_netcard_index(only in 172.19.123.121)
func UpdateNetcardindex(ipaddr string, ch chan int) {
	count := 0
	var serverid string
	fmt.Println("connect database: t_server_netcard_index")
	db, err := sql.Open("mysql", "kingyp_ustc:123456@tcp(172.19.123.121:3306)/srvadmin?charset=utf8")
	defer db.Close()
	checkErr(err)
	err = db.QueryRow("SELECT server_id FROM t_server_netcard WHERE ipaddr=?", ipaddr).Scan(&serverid)
	switch {
	case err == sql.ErrNoRows:
		log.Printf("No user with that ID.")
	case err != nil:
		log.Fatal(err)
	default:
		fmt.Printf("serverid is %s\n", serverid)
	}
	ifDescrs := []string{}
	rows, err := db.Query("select name from t_server_netcard where server_id=?", serverid)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	for rows.Next() {
		var name string
		if err := rows.Scan(&name); err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s\n", name)
		ifDescrs = append(ifDescrs, name)
	}
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
	fmt.Println(len(ifDescrs))

	g.Default.Target = ipaddr
	err = g.Default.Connect()
	if err != nil {
		log.Fatalf("Connect() err:%v", err)
	}

	oidnums := []string{"1.3.6.1.2.1.2.1.0"}
	var ifDescrNums int64 = 0
	defer g.Default.Conn.Close()
	result, err2 := g.Default.Get(oidnums)
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}
	for _, variable := range result.Variables {
		switch variable.Type {
		case g.OctetString:
			fmt.Printf("************string: %s\n", string(variable.Value.([]byte)))
		default:
			ifDescrNums = (g.ToBigInt(variable.Value)).Int64()
			fmt.Printf("number: %d\n", ifDescrNums)
		}
	}

	oids := []string{}
	for i := 1; i <= int(ifDescrNums); i++ {
		var strs string
		strs = "1.3.6.1.2.1.2.2.1.2." + strconv.Itoa(i)
		//		fmt.Println(strs)
		oids = append(oids, strs)
	}
	ifDescrIndex := make(map[string]int, int(ifDescrNums))
	result, err3 := g.Default.Get(oids)
	if err3 != nil {
		log.Fatalf("Get() err: %v", err3)
	}
	for i, variable := range result.Variables {
		switch variable.Type {
		case g.OctetString:
			var ifname string
			ifname = string(variable.Value.([]byte))
			//fmt.Printf("************string: %s\n", ifname)
			ifDescrIndex[ifname] = i + 1
		default:
			tmp := (g.ToBigInt(variable.Value)).Int64()
			fmt.Println(tmp)
		}
	}
	fmt.Println(ifDescrIndex)

	for i, v := range ifDescrs {
		index, ok := ifDescrIndex[v]
		if !ok {
			log.Fatalf("get %s err", v)
		}
		fmt.Println(i, index)
		var t_name string
		err = db.QueryRow("SELECT name FROM t_server_netcard_index WHERE server_id=? and name=?", serverid, v).Scan(&t_name)
		switch {
		case err == sql.ErrNoRows:
			fmt.Printf("No user with that ID.")
			//insert
			stmt, err := db.Prepare("INSERT t_server_netcard_index SET server_id=?,name=?,ifDescrIndex=?")
			checkErr(err)

			res, err := stmt.Exec(serverid, v, index)
			checkErr(err)

			_, err = res.LastInsertId()
			checkErr(err)

			//			fmt.Println(id)
		case err != nil:
			log.Fatal(err)
		default:
			stmt, err := db.Prepare("UPDATE t_server_netcard_index SET ifDescrIndex=? WHERE server_id=? and name=?")
			checkErr(err)

			res, err := stmt.Exec(index, serverid, v)
			checkErr(err)

			_, err = res.RowsAffected()
			checkErr(err)
			//			fmt.Println(affect)
		}

	}

	ch <- count
}