I've done some simpler scripts successfully to parse data from json to import into InfluxDB (and to view in Grafana), but this one is much trickier than what I'm used to. Cisco UCCX/Finesse's VoiceCSQDetailsStats API looks like the below json. What I want to do is to have a loop going to parse the below json, and poll the individual user data (agentId, agentName, agentState, and agentState Duration based on each id) to InfluxDB using the bottom curls. How would I go about accomplishing this with bash?
Save the data below as the following variables:
jsonAgentId="id"
jsonAgentName="agentName"
jsonAgentState="agentState"
jsonAgentStateDuration="agentStateDuration"
VoiceCSQDetailsStats json:
{
"id": "muser",
"operation": "UPDATE",
"VoiceCSQDetailsStats": {
"agentId": "muser",
"agentName": "My User",
"agentState": "Not Ready",
"skillGroup": "",
"agentStateDuration": 982761,
"reason": "Break",
"AgentVoiceCSQNames": [
{
"agentVoiceCSQName": "Dispatcher"
},
{
"agentVoiceCSQName": "NOCEscalation"
},
{
"agentVoiceCSQName": "NOCHelpdesk"
}
]
}
}
{
"id": "yuser",
"operation": "UPDATE",
"VoiceCSQDetailsStats": {
"agentId": "yuser",
"agentName": "Your User",
"agentState": "Talking",
"skillGroup": "",
"agentStateDuration": 626160,
"reason": "",
"AgentVoiceCSQNames": [
{
"agentVoiceCSQName": "Dispatcher"
},
{
"agentVoiceCSQName": "NOCHelpdesk"
}
]
}
}
{
"id": "euser",
"operation": "UPDATE",
"VoiceCSQDetailsStats": {
"agentId": "euser",
"agentName": "Everyones User",
"agentState": "Ready",
"skillGroup": "",
"agentStateDuration": 203631,
"reason": "",
"AgentVoiceCSQNames": [
{
"agentVoiceCSQName": "NOCHelpdesk"
},
{
"agentVoiceCSQName": "NOCEscalation"
}
]
}
}
{
"id": "duser",
"operation": "UPDATE",
"VoiceCSQDetailsStats": {
"agentId": "duser",
"agentName": "Dumb User",
"agentState": "Not Ready",
"skillGroup": "",
"agentStateDuration": 175342,
"reason": "Call Not Answered",
"AgentVoiceCSQNames": [
{
"agentVoiceCSQName": "NOCEscalation"
},
{
"agentVoiceCSQName": "NOCHelpdesk"
}
]
}
}
{
"id": "fuser",
"operation": "UPDATE",
"VoiceCSQDetailsStats": {
"agentId": "fuser",
"agentName": "Foolish User",
"agentState": "Not Ready",
"skillGroup": "",
"agentStateDuration": 1057520,
"reason": "Offhook",
"AgentVoiceCSQNames": [
{
"agentVoiceCSQName": "NOCEscalation"
},
{
"agentVoiceCSQName": "NOCHelpdesk"
}
]
}
}
{
"id": "druser",
"operation": "UPDATE",
"VoiceCSQDetailsStats": {
"agentId": "druser",
"agentName": "Drug User",
"agentState": "Talking ( from CSQ: NOCHelpdesk )",
"skillGroup": "NOCHelpdesk",
"agentStateDuration": 167914,
"reason": "Offhook",
"AgentVoiceCSQNames": [
{
"agentVoiceCSQName": "NOCEscalation"
},
{
"agentVoiceCSQName": "NOCHelpdesk"
}
]
}
}
Send to InfluxDB:
curl -i -XPOST "$influxdbIP:8086/write?db=nocdb" --data-binary "uccxstats,host=uccx,user=$jsonAgentId value=$jsonAgentName"
curl -i -XPOST "$influxdbIP:8086/write?db=nocdb" --data-binary "uccxstats,host=uccx,user=$jsonAgentId value=$jsonAgentState"
curl -i -XPOST "$influxdbIP:8086/write?db=nocdb" --data-binary "uccxstats,host=uccx,user=$jsonAgentId value=$jsonAgentStateDuration"
Using jq, I had considered doing the below, but I'll still need for it to loop through the users and stop once jq -c '.[n]' returns null.
curl 'http://10.10.66.16:9080/realtime/VoiceCSQDetailsStats' | jq -c '.[6]' | grep -oP '(?<="id":")[^."]*'
druser
curl 'http://10.10.66.16:9080/realtime/VoiceCSQDetailsStats' | jq -c '.[6]' | grep -oP '(?<="agentName":")[^."]*'
Drug User
curl 'http://10.10.66.16:9080/realtime/VoiceCSQDetailsStats' | jq -c '.[6]' | grep -oP '(?<="agentState":")[^."]*'
Ready
curl 'http://10.10.66.16:9080/realtime/VoiceCSQDetailsStats' | jq -c '.[6]' | grep -oP '(?<="agentStateDuration":)[^.,]*'
167914
curl 'http://10.10.66.16:9080/realtime/VoiceCSQDetailsStats' | jq -c '.[6]' | grep -oP '(?<="reason":")[^."]*'
Offhook