Example #1
0
 public Tuple<List<statistics>, List<networkinterface>, List<volume>, node> DoPool(node n = null, List<networkinterface> nics = null, List<volume> volumes = null)
 {
     if (n == null)
     {
         n = new node() { name = host };
     }
     if (nics == null)
     {
         nics = new List<networkinterface>();
     }
     if (volumes == null)
     {
         volumes = new List<volume>();
     }
     var performancestats = FitToNodeInfo(DoSystemUniquePool(), n, nics, volumes).ToList();
     nics.ForEach(ni => ni.nodeid = n.id);
     volumes.ForEach(v => v.nodeid = n.id);
     var translatedstats = TranslateToStats(performancestats, n, nics, volumes).ToList();
     return Tuple.Create(translatedstats, nics, volumes, n);
 }
Example #2
0
 protected virtual IEnumerable<statistics> TranslateToStats(IEnumerable<statistics> systemstats, node n, List<networkinterface> nics, List<volume> volumes)
 {
     foreach (var s in systemstats) yield return s;
 }
Example #3
0
        protected override IEnumerable<statistics> TranslateToStats(IEnumerable<statistics> systemstats, node n, List<networkinterface> nics, List<volume> volumes)
        {
            var time = DateTime.Now;
            yield return new statistics() { nodeid = n.id, begintime = time, endtime = time, category = "MEM", type = "FREE", cap = n.TotalMemory, avg = Convert.ToDouble(n.TotalMemory - n.MemoryUsed) };
            yield return new statistics() { nodeid = n.id, begintime = time, endtime = time, category = "MEM", type = "USE", cap = n.TotalMemory, avg = Convert.ToDouble(n.MemoryUsed) };
            yield return new statistics() { nodeid = n.id, begintime = time, endtime = time, category = "CPU", type = "CPU", avg = Convert.ToDouble(n.CPULoad) };

            nics.RemoveAll(ni => string.IsNullOrWhiteSpace(ni.NetworkAddress) == true);
            foreach (var ni in nics)
            {
                ni.IsTxRxMode = true;
                ni.TxBytes = ni.OutBps;
                ni.RxBytes = ni.InBps;
                ni.OutBps = 0;
                ni.InBps = 0;
                yield return new statistics() { nodeid = n.id, begintime = time, endtime = time, category = "NETWORK", type = "OUTBPS", avg = Convert.ToDouble(ni.OutBps), val = ni.caption };
                yield return new statistics() { nodeid = n.id, begintime = time, endtime = time, category = "NETWORK", type = "INBPS", avg = Convert.ToDouble(ni.InBps), val = ni.caption };
            }
        }
Example #4
0
        static IEnumerable<TSDBData> HostInfoToTSDB(hostinfo info, string metricHeader)
        {
            Tuple<List<statistics>, List<networkinterface>, List<volume>, node> pooledinfo = null;
            var nodeinfo = new node() { name = info.host, status = (int)Hict.node.NodeStatus.Active, };
            var nics = new List<networkinterface>();
            var volumes = new List<volume>();
            var timestamp = OpenTSDB.GetUnixTime(DateTime.UtcNow);
            var couldcontinue = true;
            try
            {
                pooledinfo = info.DoPool(nodeinfo, nics, volumes);
            }
            catch (Exception ex)
            {
                logger.Fatal("collect data from {0} raise {1}", info.host, ex.ToString());
                if (nodeinfo != null)
                    nodeinfo.status = (int)node.NodeStatus.Unreachable;
                couldcontinue = false;
            }

            if (couldcontinue == false)
            {
                yield return new TSDBData()
                {
                    metric = metricHeader + ".Status",
                    tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                    },
                    timestamp = timestamp,
                    value = -1,
                };
                yield break;
            }

            nodeinfo.PoolIntervalSeconds = (int)Pollinterval.TotalSeconds;

            nodeinfo.LastSync = DateTime.UtcNow;

            // update statictics
            foreach (var s in pooledinfo.Item1)
            {
                switch (s.category)
                {
                    case "MEM":
                        switch (s.type)
                        {
                            case "FREE":
                                yield return new TSDBData()
                                {
                                    metric = metricHeader + ".Memory.Free",
                                    tags = new Dictionary<string, string>() {
                                       {"Host",nodeinfo.name},
                                    },
                                    timestamp = timestamp,
                                    value = s.avg,
                                };
                                break;
                            case "USE":
                                yield return new TSDBData()
                                {
                                    metric = metricHeader + ".Memory.Usage",
                                    tags = new Dictionary<string, string>() {
                                       {"Host",nodeinfo.name},
                                    },
                                    timestamp = timestamp,
                                    value = s.avg,
                                };
                                break;
                        }
                        break;
                    case "CPU":
                        yield return new TSDBData()
                        {
                            metric = metricHeader + ".CPU",
                            tags = new Dictionary<string, string>() {
                                       {"Host",nodeinfo.name},
                                    },
                            timestamp = timestamp,
                            value = s.avg,
                        };
                        break;
                }
            }
            // update volumes
            foreach (var v in volumes)
            {
                var volMetric = metricHeader + ".Volume";
                yield return new TSDBData()
                {
                    metric = volMetric + ".Usage",
                    tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                        {"Volume",FixMetricName(v.name)},
                    },
                    timestamp = timestamp,
                    value = v.usage,
                };

                yield return new TSDBData()
                {
                    metric = volMetric + ".Capacity",
                    tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                        {"Volume",FixMetricName(v.name)},
                    },
                    timestamp = timestamp,
                    value = v.capacity,
                };
            }

            // update network interfaces.

            foreach (var n in nics)
            {
                var nicMetric = metricHeader + ".NIC";
                if (n.IsTxRxMode)
                {
                    yield return new TSDBData()
                    {
                        metric = nicMetric + ".RxBytes",
                        tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                        {"NIC", FixMetricName(n.caption)},
                    },
                        timestamp = timestamp,
                        value = n.RxBytes
                    };

                    yield return new TSDBData()
                    {
                        metric = nicMetric + ".TxBytes",
                        tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                        {"NIC", FixMetricName(n.caption)},
                    },
                        timestamp = timestamp,
                        value = n.TxBytes
                    };
                }
                else
                {
                    yield return new TSDBData()
                    {
                        metric = nicMetric + ".InBps",
                        tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                        {"NIC", FixMetricName(n.caption)},
                    },
                        timestamp = timestamp,
                        value = n.InBps
                    };

                    yield return new TSDBData()
                    {
                        metric = nicMetric + ".OutBps",
                        tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                        {"NIC", FixMetricName(n.caption)},
                    },
                        timestamp = timestamp,
                        value = n.OutBps
                    };
                }
            }

            yield return new TSDBData()
            {
                metric = metricHeader + ".Status",
                tags = new Dictionary<string, string>() {
                        {"Host",nodeinfo.name},
                    },
                timestamp = timestamp,
                value = 1,
            };
        }
Example #5
0
        protected override IEnumerable<statistics> FitToNodeInfo(IEnumerable<statistics> systemstats, node n, List<networkinterface> nics, List<volume> volumes)
        {
            var stats = systemstats.ToList();
            int cpucores = 1;
            StatisticsType st;
            double dvalue;

            // first round, init some values
            foreach (var s in stats)
            {
                if (Enum.TryParse(s.type, out st))
                {
                    switch (st)
                    {
                        case StatisticsType.MachineType:
                            n.MachineType = s.val;
                            yield return s;
                            break;
                        case StatisticsType.DMI:
                            n.Manufacturer = s.val;
                            if (s.val.Contains("VirtualBox"))
                                n.IsVMHost = 1;
                            yield return s;
                            break;
                        case StatisticsType.UpSeconds:
                            n.LastBoot = DateTime.UtcNow.AddSeconds(Convert.ToDouble(s.val) * -1);
                            yield return s;
                            break;
                        case StatisticsType.MemInfoTotal:
                            if (double.TryParse(s.val, out dvalue))
                            {
                                n.TotalMemory = (float)dvalue;
                                yield return s;
                            }
                            break;
                        case StatisticsType.CPUCores:
                            if (double.TryParse(s.val, out dvalue))
                            {
                                cpucores = (int)dvalue;
                                yield return s;
                            }
                            break;
                        case StatisticsType.VolumeTotal:
                            if (double.TryParse(s.val, out dvalue))
                            {
                                var foundv = volumes.FirstOrDefault(v => v.name == s.category);
                                if (foundv == null)
                                {
                                    foundv = new volume() { name = s.category };
                                    volumes.Add(foundv);
                                }
                                foundv.capacity = (float)dvalue * (float)1024;
                                foundv.LastSync = DateTime.UtcNow;
                                yield return s;
                            }
                            break;
                        case StatisticsType.InterfaceIp:
                            var foundnic = nics.FirstOrDefault(nic => nic.caption == s.category);
                            if (foundnic == null)
                            {
                                foundnic = new networkinterface() { caption = s.category };
                                nics.Add(foundnic);
                            }
                            foundnic.LastSync = DateTime.UtcNow;
                            foundnic.NetworkAddress = s.val;
                            yield return s;
                            break;
                        case StatisticsType.InterfaceRx:
                            var foundrxnic = nics.FirstOrDefault(nic => nic.caption == s.category);
                            if (foundrxnic == null)
                            {
                                foundrxnic = new networkinterface() { caption = s.category };
                                nics.Add(foundrxnic);
                            }
                            float inbps;
                            if (float.TryParse(s.val, out inbps))
                            {
                                foundrxnic.LastSync = DateTime.UtcNow;
                                foundrxnic.InBps = inbps;
                            }
                            yield return s;
                            break;
                        case StatisticsType.InterfaceTx:
                            var foundtxnic = nics.FirstOrDefault(nic => nic.caption == s.category);
                            if (foundtxnic == null)
                            {
                                foundtxnic = new networkinterface() { caption = s.category };
                                nics.Add(foundtxnic);
                            }
                            float outbps;
                            if (float.TryParse(s.val, out outbps))
                            {
                                foundtxnic.LastSync = DateTime.UtcNow;
                                foundtxnic.OutBps = outbps;
                            }
                            yield return s;
                            break;
                    }
                }
            }

            // second round.
            foreach (var s in stats)
            {
                if (Enum.TryParse(s.type, out st))
                {
                    switch (st)
                    {
                        case StatisticsType.CPUUsage:
                            double d;
                            if (double.TryParse(s.val, out d))
                            {
                                n.CPULoad = (int)(d / (double)cpucores);
                                yield return s;
                            }
                            break;
                        case StatisticsType.MEMUsage:
                            if (double.TryParse(s.val, out dvalue))
                            {
                                n.MemoryUsed = (float)(dvalue * n.TotalMemory /100);
                                yield return s;
                            }
                            break;

                        case StatisticsType.VolumeUsage:
                            if (double.TryParse(s.val, out dvalue))
                            {
                                var foundv = volumes.FirstOrDefault(v => v.name == s.category);
                                if (foundv != null)
                                {
                                    foundv.LastSync = DateTime.UtcNow;
                                    foundv.usage = (float)dvalue * (float)1024;
                                    yield return s;
                                }
                            }
                            break;
                    }
                }

            }

            n.status = (int)Hict.node.NodeStatus.Active;
            n.Ip = string.Join(",", nics.Select(nic => nic.NetworkAddress).ToArray());
        }