4
\$\begingroup\$

The idea is to read a file - "data" - and write the information to a new file - "output" - only if it meets the criteria (if the resource usage is more than a configured threshold).

Content of the file "data":

Namespace:              env-demo
Resource                Used            Hard
--------                ----            ----
cpu                     14500m          16
memory                  16857746636800m 32Gi
persistentvolumeclaims  7               9
pods                    12              24
replicationcontrollers  31              36
resourcequotas          1               10
secrets                 12              18
services                17              24


********************
Name:                   2xlarge
Namespace:              env-stg
Resource                Used            Hard
--------                ----            ----
cpu                     12              16
memory                  28306127360     32Gi
persistentvolumeclaims  8               9
pods                    17              24
replicationcontrollers  34              40
resourcequotas          1               10
secrets                 13              18
services                20              24

********************

The program looks like this:

import groovy.transform.Field

//reading the data file
File result = new File("data")
//writes required information to a new file
def output = new File("output")
def line
@Field def Threshold = 0.8

resource_list = [
                      "cpu","memory", "persistentvolumeclaims", "pods", \
                      "replicationcontrollers", "resourcequotas", "secrets", "services"
                ]

//method to process the data and returns only if a resource usage is
//above threshold
def process_data(resource){
    //check if the resource is part of either cpu or memory
    if ( resource[0] in resource_list.take(2)){
       //check if above threshold        
       if ( (resource[2].take(2).toInteger() * Threshold) <= (resource[1].take(2).toInteger())){
           return "${resource[0]} " + "utlization is above threshold with usage of " + "${resource[1]} \n"
       }
    }
    else if ( (resource[2].toInteger() * Threshold) <= (resource[1].toInteger()) ) {
        return "${resource[0]} " + "utlization is above threshold with usage of " + "${resource[1]} \n"
    }
}

result.withReader { reader ->
    while ((line = reader.readLine()) != null) {
        try {
            // find out the project name
            if ("Namespace" == "${line}".split(':')[0]){
                output.append "#################################### \n"
                output.append "${line}".split(':')[1].trim() + "\n"
                output.append "#################################### \n"
            }
            // doing minus do avoid NULL characters
            def resource = "${line}".split('\t').minus('')
            if (resource[0] in resource_list){
                def res = process_data(resource)
                output.append res.minus('null')
            }
        }
        catch (all){
        }

    }
}

The output is:

# cat output 
#################################### 
env-demo
#################################### 
cpu utlization is above threshold with usage of 14500m 
replicationcontrollers utlization is above threshold with usage of 31 
#################################### 
env-stg
#################################### 
memory utlization is above threshold with usage of 28306127360 
persistentvolumeclaims utlization is above threshold with usage of 8 
replicationcontrollers utlization is above threshold with usage of 34 
services utlization is above threshold with usage of 20 

I have written this program by taking the inputs by googling.

Please let me know what improvements should be done in this program.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can simplify result.withReader { reader -> while ((line = reader.readLine()) != null) { } } with just result.eachLine { String line -> }. Also replace "${line}" with just line, it is already a String, no need to create a new String each time you use it. \$\endgroup\$ Commented Jan 3, 2023 at 9:48
  • 1
    \$\begingroup\$ Also, the whole point of putting variables in quotes is avoid having to concatenate them. Simplify this, return "${resource[0]} " + "utlization is above threshold with usage of " + "${resource[1]} \n", by doing this, return "${resource[0]} utlization is above threshold with usage of ${resource[1]} \n". \$\endgroup\$ Commented Jan 3, 2023 at 9:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.