# Adding processed fields with metricbeat processors

**URL:** <https://discuss.elastic.co/t/adding-processed-fields-with-metricbeat-processors/270012>\
**Category:** Beats\
**Tags:** metricbeat\
**Created:** [April 13, 2021, 1:27pm UTC](https://discuss.elastic.co/t/adding-processed-fields-with-metricbeat-processors/270012 "2021-04-13T13:27:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_x/32/86703_2.png) [@m\_x](https://discuss.elastic.co/u/m_x)\
**Post date:** [April 13, 2021, 1:27pm UTC](https://discuss.elastic.co/t/adding-processed-fields-with-metricbeat-processors/270012/1 "2021-04-13T13:27:54Z")

</div>

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 :

```auto
    - 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

---

<div class="post-metadata">

**Author:** ![m\_x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_x/32/86703_2.png) [@m\_x](https://discuss.elastic.co/u/m_x)\
**Post date:** [April 14, 2021, 8:12am UTC](https://discuss.elastic.co/t/adding-processed-fields-with-metricbeat-processors/270012/2 "2021-04-14T08:12:35Z")

</div>

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

```auto
    - 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);
            }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 12, 2021, 10:13am UTC](https://discuss.elastic.co/t/adding-processed-fields-with-metricbeat-processors/270012/3 "2021-05-12T10:13:15Z")

</div>

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