简体   繁体   English

JSON Fusioncharts和Shell脚本

[英]json fusioncharts and shell script

Hoping someone can help guide me in the right direction. 希望有人可以指导我正确的方向。

We have an open source monitoring system (Nagios) that utilizes rrdtool solution for graphs. 我们有一个开源监视系统(Nagios),它使用rrdtool解决方案来绘制图形。 While that's great and all I want to be able to rrdfetch or rrdexport data out via the nagios shell scripts and encode those into json to leverage a cleaner looking graphing solution such as Fusioncharts. 虽然那很棒,但我希望能够通过nagios shell脚本将数据rrdfetch或rrdexport导出,并将它们编码为json,以利用更清晰的图形化解决方案,例如Fusioncharts。

I'm not much of a web admin so I am fumbling through looking for the right approach. 我不是网络管理员,因此我正在寻找正确的方法。 What I have experimented with: 我尝试过的是:

taking two variable outputs via rrd exports and producing: 通过rrd导出获取两个变量输出并产生:

echo "$label"

    22:30:00
    23:00:00
    23:30:00
    00:00:00

echo "$value"
    2.47
    2.68
    3.40
    2.43

What is the best way to get that into a json format so a solution like fusioncharts can consume it? 将其转换为json格式的最佳方法是什么,以便Fusioncharts之类的解决方案可以使用它?

I have been looking at a solution called jq , which seems promising when I test / google some examples but in all honesty it's a little over my head. 我一直在寻找一个名为jq的解决方案,当我测试/谷歌一些示例时,它似乎很有前途,但老实说,这有点让我烦恼。

jq -n "{label:\"$cat1\"}"
jq -n "{value:\"$avg1\"}"

{
  "label": "22:30:00 23:00:00 23:30:00 00:00:00"
}

{
  "value": "2.47 2.68 3.40 2.43"
}

In jsfiddle I was trying to get the shell script to output a json format like this fusionchart example. 在jsfiddle中,我试图获取shell脚本来输出json格式的文本,例如Fusionchart示例。

FusionCharts.ready(function() {
    var revenueChart = new FusionCharts({
        type: 'line',
        renderAt: 'chart-container',
        width: '500',
        height: '300',
        dataFormat: 'json',
        id: 'revenue-chart',
        dataSource: {
            "chart": {
                "caption": "Sample avg times",
                "subCaption": "Sample avg times",
                "xAxisName": "time",
                "yAxisName": "avg",
                "theme": "fint",
                "rotateValues": "0"
            },

            "data": [{
                "label": "22:30:00",
                "value": "2.47"
            }, {
                "label": "23:00:00",
                "value": "2.68"
            }, {
                "label": "23:30:00",
                "value": "3.40"
            }, {
                "label": "00:00:00",
                "value": "2.43"
            },]
        }
    });

If you want to interpolate some values into a static piece of text, have a look at here documents. 如果要将某些值插值到静态文本中,请查看此处的文档。

cat <<HERE
  some static text here;
  $label
  some more static text;
  or even $(code to dynamically extract value) in the middle
  of some other text
HERE

You can easily glue together parts, too: 您也可以轻松地将零件粘合在一起:

printf 'static\nheader\ntext'
jq something |
while read -r snippet; do  # reads one line from jq at a time
    printf 'stuff around %s goes here\n' "$snippet"
done
echo 'static
tail
can be multi-line string, too!'
tr ':' '\n' <<<"multi:line:string:with:$value:in:it"  # here string, bash only

Whether you use cat with a here document, or a printf (or just plain echo , or, say, date with a creative format string -- actually, anything which produces text on standard output) to produce the string doesn't really matter from a performance or usability point of view; 无论您将cat与此处文档一起使用,还是与printf (或仅使用普通echo ,或者将date与创意格式的字符串一起使用-实际上,任何会在标准输出上生成文本的东西)都不会影响字符串性能或可用性的观点; use whichever feels the most natural. 使用最自然的感觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM