Puppet Function: peadm::convert_status

Defined in:
functions/convert_status.pp
Function type:
Puppet Language

Summary

Transforms a value in a human readable status with or without colors

Overview

peadm::convert_status(Variant[String,Boolean, Integer] $status, Optional[Integer] $total = 0, Optional[Boolean] $use_colors = true)String

Examples:

With colors

peadm::convert_status(true) = "\e[32moperational\e[0m"

Without colors

peadm::convert_status(true, 0, false) = "operational"

Using integers where 1 of 2 services has failed

peadm::convert_status(1, 2, false) = "degraded"

Using integers where 2 of 2 services has failed

peadm::convert_status(2, 2, false) = "failed"

Using integers where 0 of 2 services has failed

peadm::convert_status(0, 2, false) = "operational"

Parameters:

  • status (Variant[String,Boolean, Integer])

    A value of true, false, degraded, or an Integer that represents number of non operationally services If using an integer, you must also supply the total amount of services

  • total (Optional[Integer]) (defaults to: 0)

    the total number of services, used only when the status is an integer

  • use_colors (Optional[Boolean]) (defaults to: true)

    Adds colors to the status, defaults to true

Returns:

  • (String)

    A status as a string with or without color



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'functions/convert_status.pp', line 17

function peadm::convert_status(
  Variant[String,Boolean, Integer] $status,
  Optional[Integer] $total = 0,
  Optional[Boolean] $use_colors = true
) >> String {
  if $status =~ Integer {
    if ( $status < 1 ) {
      $result = 'operational'
    } elsif $status == $total {
      $result = 'failed'
    } else {
      $result = 'degraded'
    }
  } else {
    $result = $status ? {
      true               => 'operational',
      false              => 'failed',
      /degraded/         => 'degraded',
      default            => 'unknown'
    }
  }
  if $use_colors {
    $color = $result ? {
      'degraded' => 'yellow',
      'failed'   => 'red',
      'operational' => 'green',
      default => 'yellow'
    }
    format::colorize($result, $color)
  } else {
    $result
  }
}