Adding processed fields with metricbeat processors

I would like to add some fields to the data sent by Metricbeat. My machines have a hostname like this AA-BBB-CCXX where A, B, C are letters and X numbers. I would like to have :

host.group: "AA-BBB-CC"
host.number: "XX"

Metricbeat already sends host.name containing the full hostname.

I have tried this in system.yml using the script processor :

    - module: system
      period: 10s
      metricsets:
        - process
      processors:
        - add_tags:
            tags: [session_stats]
     - script:
          lang: javascript
          id: host_metadata
          source: >
            function process(event) {
                var hostn = event.Get("hostname"); // does not work
                             
                var regex_num_end = /[0-9]+$/g;
                var host_number = hostn.match(regex_num_end)[0];
                var host_group = hostn.replace(regex_num_end, "");
                
                event.Put("host.number",host_number);
                event.Put("host.group",host_group);
            }

event.Get("host.name") does not work either and return null. The code works if I hardcode the hostname though.
I also tried to use the dissect processor without success.

Thanks

I found a way around (probably not very clean / right) using the environment variable capability of the configuration :

    - script:
          lang: javascript
          id: host_metadata
          params:
            computername: '${COMPUTERNAME}'       
          source: >
            var params = {computername: ""};
            
            function register(scriptParams) {
                params = scriptParams;
            }
            
            function process(event) {
                var hostn = params.computername;
                                
                var regex_num_end = /[0-9]+$/g;
                var host_number = hostn.match(regex_num_end)[0];
                var host_group = hostn.replace(regex_num_end, "");
                
                event.Put("host.number",host_number);
                event.Put("host.group",host_group);
            }

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