逃脱“在JSON里面的bash中

时间:2019-02-13 12:22:02

标签: json bash

这可能是我想念的一件容易的事,但是我现在有一个用于单个主机的脚本,我想为所有主机创建相同的脚本。

下面是我正在使用的代码

    {while read -r host
do

ho=$host
an=some_alert_$host
tick="// Host of the machine\nvar host = '"$ho"'\n\n// durations\nvar period = 7m\n\nvar every = 6m\n\n// alerts\nvar warn = 75\n\nvar crit = 85\n\n// database\nvar database = 'xxx'\n\n// measurement from where data is coming\nvar measurement = 'disk'\n\n// RP from where data is coming\nvar RP = 'autogen'\n\n// which influx cluster to use\nvar clus = 'xxx'\n\nvar alertName = '"$an"'\n\nvar triggerType = 'threshold'\n\nbatch\n    |query('''SELECT last(\"used_percent\") as \"value\" FROM \"''' + string(database) + '''\".\"''' + string(RP) + '''\".\"''' + string(measurement) + '''\" where host = \"''' + string(host) + '''\" ''')\n        .cluster(clus)\n        .period(period)\n        .every(every)\n        .groupBy(*)\n        .align()\n    |alert()\n        .warn(lambda: \"value\" \u003e warn)\n        .crit(lambda: \"value\" \u003e crit)\n        .stateChangesOnly()\n        .message(' {{ index .Tags \"path\" }} used {{ index .Fields \"value\" | printf \"%0.2f\" }}% disk space on {{ .Time.Local.Format \"2006.01.02 - 15:04:05\" }} in {{ index .Tags \"host\" }} ')\n        .details('''\n\n \u003cpre\u003e\n ------------------------------------------------------------------\n CLIENT NAME      : xxx\n ENVIRONMENT      : Prod\n DEVICE TYPE      : {{ index .Tags \"os\" }}\n APPLICATION NAME : {{ index .Tags \"app_stack\" }}\n HOST NAME        : {{ index .Tags \"host\" }}\n IP ADDRESS       : {{ index .Tags \"ip\" }}\n DATE             : {{ .Time.Local.Format \"2006.01.02 - 15:04:05\" }}\n ITEM NAME        : {{ index .Tags \"path\" }} Disk Utilization (%)\n VALUE            : {{ index .Fields \"value\" | printf \"%0.2f\" }} %\n SEVERITY         : {{.Level}}\n ------------------------------------------------------------------\n \u003c/pre\u003e\n\t\n''')\n        .log('/tmp/chronograf/$an.log')\n        .levelTag('level')\n        .idTag('id')\n        .messageField('message')\n        .email()\n        .to('xxx')\n    |influxDBOut()\n        .database('chronograf')\n        .retentionPolicy(RP)\n        .measurement('alerts')\n        .tag('alertName', alertName)\n" 


echo "{
  \"id\": "some_alert_$host",
  \"type\": \"batch\",
  \"dbrps\": [
    {
      \"db\": \"kenan\",
      \"rp\": \"autogen\",
      \"name\": \"kenan.autogen\"
    }
  ],
  \"tickscript\": "$tick"
}"

done < host}

在这里,tick变量是第一个脚本,但是当我在json中调用tick变量时,它会删除“。”前面的\。

1 个答案:

答案 0 :(得分:3)

不要尝试手动应用JSON编码,尤其是与插值结合使用时。使用jq生成JSON;它将对所有内容进行编码,让您将tick定义为普通字符串。

while read -r host
do

  ho=$host
  an=some_alert_$host

# Generate the script as you normally would, not as a JSON string
  tick="// Host of the machine
var host = '$ho'
// durations
var period = 7m

var every = 6m

// alerts
var warn = 7

var crit = 85

//etc. " 


  jq -n --arg an "$an" --arg tick "$tick" \
    '{id: $an, type: "batch", dbrps: [{db: "kenan", rp: "autogen", name: "kenan.autogen"}], tickscript: $tick}'


done < host

您可能还想在定义ho之前清除tick的值,以确保结果确实是有效的脚本。