func swIfMetrics() (L []*model.MetricValue) { initVariable() allIp := AllSwitchIp() chs := make([]chan ChIfStat, len(allIp)) limitCh := make(chan bool, g.Config().Switch.LimitConcur) startTime := time.Now() log.Printf("UpdateIfStats start. The number of concurrent limited to %d. IP addresses number is %d", g.Config().Switch.LimitConcur, len(allIp)) for i, ip := range allIp { chs[i] = make(chan ChIfStat) limitCh <- true go coreSwIfMetrics(ip, chs[i], limitCh) time.Sleep(5 * time.Millisecond) } for _, ch := range chs { chIfStat := <-ch if chIfStat.PingResult == true && !slice.ContainsString(AliveIp, chIfStat.Ip) { AliveIp = append(AliveIp, chIfStat.Ip) } if chIfStat.IfStatsList != nil { if g.Config().Debug { log.Println(chIfStat.Ip, chIfStat.PingResult, len(*chIfStat.IfStatsList), chIfStat.UseTime) } for _, ifStat := range *chIfStat.IfStatsList { ifNameTag := "ifName=" + ifStat.IfName ifIndexTag := "ifIndex=" + strconv.Itoa(ifStat.IfIndex) ip := chIfStat.Ip L = append(L, CounterValueIp(ifStat.TS, ip, "switch.if.In", ifStat.IfHCInOctets, ifNameTag, ifIndexTag)) L = append(L, CounterValueIp(ifStat.TS, ip, "switch.if.Out", ifStat.IfHCOutOctets, ifNameTag, ifIndexTag)) //如果IgnorePkt为false,采集Pkt if g.Config().Switch.IgnorePkt == false { L = append(L, CounterValueIp(ifStat.TS, ip, "switch.if.InPkts", ifStat.IfHCInUcastPkts, ifNameTag, ifIndexTag)) L = append(L, CounterValueIp(ifStat.TS, ip, "switch.if.OutPkts", ifStat.IfHCOutUcastPkts, ifNameTag, ifIndexTag)) } } } } endTime := time.Now() log.Printf("UpdateIfStats complete. Process time %s. Number of active ip is %d", endTime.Sub(startTime), len(AliveIp)) if g.Config().Debug { for i, v := range AliveIp { log.Println("AliveIp:", i, v) } } return }
func coreSwIfMap(ip string, limitch chan bool) { pingResult := pingCheck(ip) snmpWalk := false if !pingResult { DeletePortMap(ip) <-limitch return } vendor, err := sw.SysVendor(ip, community, snmpTimeout) if err != nil { return } if !slice.ContainsString(AliveIp, ip) { AliveIp = append(AliveIp, ip) } snmpWalk = true log.Println("port map add ", ip, vendor) if !snmpWalk { MapIfName(ip) } else { MapIfNameWalk(ip) } }
func IsTrustable(remoteAddr string) bool { ip := remoteAddr idx := strings.LastIndex(remoteAddr, ":") if idx > 0 { ip = remoteAddr[0:idx] } if ip == "127.0.0.1" { return true } return slice.ContainsString(TrustableIps(), ip) }
// filter function func filter(content_type string, raw_url string) bool { ret := false if strings.Contains(content_type, "text/plain") || strings.Contains(content_type, "application/x-gzip") { url_parsed, _ := url.Parse(raw_url) path := url_parsed.Path t := strings.Split(path[1:], ".") suffix := t[len(t)-1] if !slice.ContainsString(static_resource, suffix) { ret = true } } return ret }
func PingMetrics() (L []*model.MetricValue) { allip := AllSwitchIp() chs := make([]chan SwPing, len(allip)) for i, ip := range allip { if ip != "" { chs[i] = make(chan SwPing) go pingMetrics(ip, chs[i]) } } for _, ch := range chs { swPing := <-ch if swPing.Ping > 0 && !slice.ContainsString(AliveIp, swPing.Ip) { AliveIp = append(AliveIp, swPing.Ip) } ipTag := "ip=" + swPing.Ip L = append(L, GaugeValueIp(time.Now().Unix(), swPing.Ip, "switch.Ping", swPing.Ping, ipTag)) } return L }
func compareState() { desiredState, err := getDesiredState() if err != nil { log.Println("[ERROR] get desired state fail:", err) return } debug := g.Config().Debug if debug { log.Println("comparing......") } if len(desiredState) == 0 { if debug { log.Println("no desired app. do nothing") } // do nothing. return } newAppSlice := []string{} for name, app := range desiredState { if !g.RealState.RealAppExists(name) { if debug && app.InstanceCnt > 0 { log.Println("[=-NEW-=]:", name) } newAppSlice = append(newAppSlice, name) createNewContainer(app, app.InstanceCnt) } } realNames := g.RealState.Keys() for ii, name := range realNames { if debug { log.Printf("#%d: %s", ii, name) } if slice.ContainsString(newAppSlice, name) { continue } app, exists := desiredState[name] if !exists { if debug { log.Println("[=-DEL-=]:", name) } dropApp(name) continue } sa, _ := g.RealState.GetSafeApp(name) isOld, olds := sa.IsOldVersion(app.Image) if isOld { if len(olds) > 0 || app.InstanceCnt > 0 { log.Println("[=-UPGRADE-=]") } // deploy new instances createNewContainer(app, app.InstanceCnt) // delete old instances for _, c := range olds { dropContainer(c) } continue } nowCnt := sa.ContainerCount() if nowCnt < app.InstanceCnt { if debug { log.Printf("add:%d", app.InstanceCnt-nowCnt) } createNewContainer(app, app.InstanceCnt-nowCnt) continue } if nowCnt > app.InstanceCnt { if debug { log.Printf("del:%d", nowCnt-app.InstanceCnt) } dropContainers(sa.Containers(), nowCnt-app.InstanceCnt) } } }