Kibana painless script

Hi Team,

I am trying to write painless script to calculate Average CPU usage.

Script:

(doc['system.cpu.user.pct'].value + doc['system.cpu.system.pct'].value) / doc['system.cpu.cores'].value

Getting below error when running the script.

    {
     "root_cause": [
      {
       "type": "script_exception",
       "reason": "runtime error",
       "script_stack": [
        "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.get(ScriptDocValues.java:249)",
        "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.getValue(ScriptDocValues.java:243)",
        "(doc['system.cpu.user.pct'].value + doc['system.cpu.system.pct'].value) / doc['system.cpu.cores'].value",
        "                           ^---- HERE"
       ],
       "script": "(doc['system.cpu.user.pct'].value + doc['system.cpu.system.pct'].value) / doc['system.cpu.cores'].value",
       "lang": "painless"
      }
     ],
     "type": "search_phase_execution_exception",
     "reason": "all shards failed",
     "phase": "query",
     "grouped": true,
     "failed_shards": [
      {
       "shard": 0,
       "index": "metricbeat-7.4.2-2020.05.19-000001",
       "node": "djhRc7hwRrOX6V6Bu7Mrsg",
       "reason": {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
         "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.get(ScriptDocValues.java:249)",
         "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.getValue(ScriptDocValues.java:243)",
         "(doc['system.cpu.user.pct'].value + doc['system.cpu.system.pct'].value) / doc['system.cpu.cores'].value",
         "                           ^---- HERE"
        ],
        "script": "(doc['system.cpu.user.pct'].value + doc['system.cpu.system.pct'].value) / doc['system.cpu.cores'].value",
        "lang": "painless",
        "caused_by": {
         "type": "illegal_state_exception",
         "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
        }
       }
      }
     ]
    }

Kindly help me with this issue.

Regards,
Mugil

The reason to the bottom of the error tells you what the problem is - a scripted field is executed for each document, but there are some documents not having a value for the system.cpu.user.pct field. The first thing would be to make sure the field is actually called like this. If it is,

adding

if (doc['system.cpu.user.pct'].size()==0) return null;

should filter out those documents, so the calculation is only done for the documents which have values. You probably need to do the same for the other fields you are accessing in your script.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.