예제 #1
0
        public InfluxDiagnostics ReadDiagnostics()
        {
            InfluxDiagnostics d = new InfluxDiagnostics();

            DateTime       before = DateTime.Now;
            HttpWebRequest req    = GetWebRequest("query?q=SHOW DIAGNOSTICS&rand=" + new Random().Next());

            req.Method           = "GET";
            req.ReadWriteTimeout = 5000; // only allow 5 secs to read the stats

            Dictionary <string, InfluxQueryInfo> queries = new Dictionary <string, InfluxQueryInfo>();

            using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
            {
                d.version = res.Headers.Get("X-Influxdb-Version");

                JsonTextReader reader = new JsonTextReader(
                    new StreamReader(res.GetResponseStream()));

                JToken  tok;
                JObject results = (JObject)((JArray)JObject.Load(reader).GetValue("results")).First;
                JArray  series  = (JArray)results.GetValue("series");
                foreach (JToken t in series)
                {
                    JObject sobj = (JObject)t;
                    if (sobj.TryGetValue("name", out tok))
                    {
                        if ("system".Equals(tok.Value <string>()))
                        {
                            JArray arr = sobj.GetValue("values").Value <JArray>().First.Value <JArray>();
                            d.pid = arr[0].Value <int>();

                            String now = arr[1].Value <string>();
                            String sta = arr[2].Value <string>();

                            d.started = GrafanaQuery.ToUnixTime(DateTime.Parse(sta));
                            long nowms = GrafanaQuery.ToUnixTime(DateTime.Parse(now));
                            d.deltaTS = GrafanaQuery.ToUnixTime(before) - nowms; // positive number is ahead
                        }
                    }
                }
            }
            return(d);
        }
예제 #2
0
        private void WriteDeviceItemInputParserResult(HidSharp.Reports.Input.DeviceItemInputParser parser)
        {
            while (parser.HasChanged)
            {
                int  changedIndex = parser.GetNextChangedIndex();
                var  dataValue    = parser.GetValue(changedIndex);
                uint key          = dataValue.Usages.FirstOrDefault();
                if (!values.ContainsKey(key))
                {
                    string disp = ((Usage)key).ToString();
                    if (disp.StartsWith("427"))
                    {
                        Console.WriteLine("SKIP: " + disp);
                        continue;
                    }
                    DeviceValue tmp = new DeviceValue();
                    tmp.item    = dataValue.DataItem;
                    tmp.name    = this.name + disp;
                    values[key] = tmp;
                }
                DeviceValue v = values[key];
                v.time = GrafanaQuery.ToUnixTime(DateTime.Now);
                if (v.item.ElementBits == 1)
                {
                    v.value = (dataValue.GetLogicalValue() > 0) ? true : false;
                }
                else
                {
                    v.value = dataValue.GetLogicalValue();
                }

                if (sockets != null)
                {
                    sockets.write(v);
                }
                //Console.WriteLine(v.ToJSON());
            }
        }
예제 #3
0
        public override void doFlush(MemoryStream buffer)
        {
            logger.Debug("Send to influx: " + count + " samples, " + buffer.Length + "bytes");

            count = 0;

            HttpWebRequest req = influx.GetWebRequest("write?precision=ms&db=" + database);

            req.Method = "POST";

            if (zipit)
            {
                req.ContentType = "text/plain";
                req.Headers.Add("Content-Encoding", "gzip");
            }

            if (logger.IsTraceEnabled)
            {
                logger.Trace(Encoding.ASCII.GetString(buffer.ToArray()));
            }

            using (Stream stream = req.GetRequestStream())
            {
                if (zipit)
                {
                    using (GZipStream compressionStream = new GZipStream(stream, CompressionMode.Compress))
                    {
                        buffer.WriteTo(compressionStream);
                    }
                }
                else
                {
                    buffer.WriteTo(stream);
                }
            }


            try
            {
                using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
                {
                    LastOK     = GrafanaQuery.ToUnixTime(DateTime.Now);
                    SentBytes += buffer.Length;
                    return;
                }
            }
            catch (WebException ex)  // TODO, handle Influx Errors
            {
                ErrorCount++;
                try
                {
                    StreamReader read = new StreamReader(ex.Response.GetResponseStream());
                    String       body = read.ReadToEnd();
                    if (ex.Response.ContentType.EndsWith("json"))
                    {
                        JObject v   = JObject.Parse(body);
                        string  err = v.GetValue("error").Value <string>();
                        logger.Warn("Error Writing To Influx: " + err);
                    }
                    else
                    {
                        logger.Warn("Error Writing To Influx: " + body);
                    }
                    LastERR = GrafanaQuery.ToUnixTime(DateTime.Now);
                }
                catch (Exception xx)
                {
                    logger.Warn(xx, "Error Trying to read WebException: " + ex);
                }
            }
        }