NAME

Thruk::Utils - Utilities Collection for Thruk

DESCRIPTION

Utilities Collection for Thruk

METHODS

parse_date

my $timestamp = parse_date($c, $string)

Format: 2010-03-02 00:00:00 parse given date and return timestamp

format_date

my $date_string = format_date($string, $format)

return date from timestamp in given format

format_number

my $string = format_number($number)

return number with thousands seperator

format_cronentry

my $cron_string = format_cronentry($cron_entry)

return cron entry as string

is_valid_regular_expression

my $result = is_valid_regular_expression($expression)

return true if this is a valid regular expression

calculate_overall_processinfo

my $process_info = calculate_overall_processinfo($process_info)

computes a combined status for process infos

get_start_end_for_timeperiod

my($start, $end) = get_start_end_for_timeperiod($c,
                                                $timeperiod,
                                                $smon,
                                                $sday,
                                                $syear,
                                                $shour,
                                                $smin,
                                                $ssec,
                                                $emon,
                                                $eday,
                                                $eyear,
                                                $ehour,
                                                $emin,
                                                $esec,
                                                $t1,
                                                $t2);

returns a start and end timestamp for a report date definition

get_start_end_for_timeperiod_from_param

my($start, $end) = get_start_end_for_timeperiod_from_param($c)

returns a start and end timestamp for a report date definition will use cgi params for input

get_start_end_from_date_select_params

my($start, $end) = get_start_end_from_date_select_params($c)

returns a start and end timestamp from date select, like ex.: on the showlog page

set_message

set_message($c, $style, $text, [ $details ], [$code], [$escape])
set_message($c, {
    'style'   => 'class', # usually fail_message or success_message
    'msg'     => 'text',
    'details' => 'more details',
    'code'    => 'http response code',
    'escape'  => 'flag wether html should be escaped, default is true',
  })

set a message in an cookie for later display

append_message

append_message($text)

append text to current message

ssi_include

ssi_include($c)

puts the ssi templates into the stash

read_ssi

read_ssi($c, $page, $type)

finds all ssi files for a page of the specified type and returns the ssi content. Executable ssi files are executed and the output is appended to the ssi content. Otherwise the content of the ssi file is append to the ssi content.

read_resource_file

read_resource_file($files, [ $macros ], [$with_comments])

returns a hash with all USER1-32 macros. macros can be a predefined hash.

version_compare

version_compare($version1, $version2)

compare too version strings and return 1 if v1 >= v2

combine_filter

combine_filter($operator, $filter)

combine filter by operator

set_paging_steps

set_paging_steps($c, $data)

sets the pagins stepts, needs string like:

*100, 500, 1000, all

get_exposed_custom_vars

get_exposed_custom_vars($config, [$skip_wildcards])

return combined list of show_custom_vars and expose_custom_vars. (variable names have their leading underline removed)

get_custom_vars

get_custom_vars($c, $obj, [$prefix], [$add_host])

return custom variables in a hash

set_custom_vars

set_custom_vars($c, { options... })

set stash value for all allowed custom variables

check_custom_var_list

check_custom_var_list($varname, $allowed)

returns true if custom variable name is in the list of allowed variable names

set_allowed_rows_data

set_allowed_rows_data($obj, $allowed, $allowed_list, $show_full_commandline)

set custom variables for host/service obj

get_user_data

get_user_data($c, [$username])

returns user profile data

store_user_data

store_user_data($c, $data, [$username])

store user profile data

get_global_user_data

get_global_user_data($c)

returns global user data

store_global_user_data

store_global_user_data($c, $data)

store global user data for section

logs2xls

logs2xls($c)

save excel file by background job

savexls

savexls($c)

save excel file by background job

proxifiy_url

prepend proxy url to action url which might be proxied via thruk http

returns url with optional proxy prepended

proxifiy_me

returns proxy url if possible

returns url with optional proxy prepended

get_remote_thruk_url_path

get_remote_thruk_url_path($c, $peer_key)

returns path of last http address/thruk instance in chain

get_remote_thruk_hostname

get_remote_thruk_hostname($c, $peer_key)

returns hostname and url of last none-local address

get_remote_thruk_site_name

get_remote_thruk_site_name($c, $peer_key)

returns site name of peer

get_pnp_url

get_pnp_url($c, $object)

return pnp url for object (host/service)

get_histou_url

get_histou_url($c, $object)

return histou url for object (host/service)

get_graph_url

get_graph_url($c, $object)

return graph url for object (host/service)

get_perf_image

get_perf_image($c, {
  host           => $hst,
  service        => $svc,
  start          => $start,
  end            => $end,
  width          => $width,
  height         => $height,
  source         => $source,
  resize_grafana => $resize_grafana_images,
  format         => $format,
  show_title     => $showtitle,
  show_legend    => $showlegend,
  follow         => 0/1,  # flag wether we simply redirect proxy requests or fetch them
  theme          => light/dark,
  font_color     => #12345,
  bg_color       => #12345,
})

return raw pnp/grafana image if possible. An empty string will be returned if no graph can be exported.

encode_arg_refs

encode_arg_refs($args)

returns array with replaced args, ex. replace $c with placeholder

unencode_arg_refs

unencode_arg_refs($c, $args)

returns array with replaced args, ex. replace placeholder with $c

absolute_url

returns a absolute url

expects
$VAR1 = origin url
$VAR2 = target link

get_fake_session

get_fake_session($c, [$sessionid], [$roles], [$address], $extra)

create and return fake session id along with session data for current user

get_action_url

get_action_url($c, $escape_fun, $remove_render, $action_url, $obj)

return action_url modified for object (host/service) if we use graphite escape_fun is use to escape special char (html or quotes) remove_render remove /render in action url

extract_list

extract_list($var, $separator)

return list by splitting $var by $sep ($var can be an array or string)

array_chunk

array_chunk($list, $number)

return list of <number> evenly chunked parts

array_chunk_fixed_size

array_chunk_fixed_size($list, $size)

return list of chunked parts each with $size

translate_host_status

translate_host_status($status)

return name for status

expand_duration

expand_duration($value)

returns expanded seconds from given abbreviation

possible conversions are 1w => 604800 1d => 86400 1h => 3600 1m => 60

update_cron_file

update_cron_file($c, $section, $entries)

write crontab section

update_cron_file_maintenance

update_cron_file_maintenance($c)

update maintenance cronjobs

get_cron_time_entry

get_cron_time_entry($cronentry)

return time part of crontab entry

set_user

set_user($c, %options)

set and authenticate a user

options are: { username => username of the resulting user auth_src => hint about where this user came from superuser => superuser can change to other user names, roles will not exceed initial role set internal => internal technical user can change into any user and has admin roles force => force setting new user, even if already authenticated roles => maximum set of roles keep_session => do not update current session cookie }

change_user

change_user($c, $username)

changes username to given user, cannot exceed current roles and permissions

switch_realuser

switch_realuser($uid, $groups)

switch real user and groups

check_pid_file

check_pid_file($c)

check and write pid file if none exists

restart_later

restart_later($c, $redirect_url)

restart fcgi process and redirects to given page

wait_after_reload

wait_after_reload($c, [$backend], [$timestamp])

wait up to 30 seconds till the core responds

get_cron_entries_from_param

get_cron_entries_from_param($cronentry)

return array of cron entries from param

read_data_file

read_data_file($filename, [$c])

return data for datafile

write_data_file

write_data_file($filename, $data, [$changed_only])

write data to datafile

backup_data_file

backup_data_file($filename, $targetfile, $mode, $max_backups, [$save_interval], [$force])

write data to datafile

which

which($prog)

returns path to program or undef

reduce_number

reduce_number($number, $unit, [$divisor])

return reduced number, ex 1024B -> 1KiB

get_template_variable

get_template_variable($c, $template, $variable)

return variable defined from template

precompile_templates

precompile_templates($c)

precompile and load templates into memory

beautify_diff

beautify_diff($text)

make diff output beauty

check_memory_usage

check_memory_usage($c)

check if memory limit is above the threshold

log_error_with_details

log_error_with_details($c, $message, $errorDetails)

log error along with details about url and logged in user

stop_all

stop_all()

stop all thruk pids

base_folder

base_folder($c)

return base etc folder

is_post

is_post($c)

make sure this is a post request

check_csrf

check_csrf($c)

ensure valid cross site request forgery token

get_plugin_name

get_plugin_name(__FILE__, __PACKAGE__)

returns the name of the plugin

backends_list_to_hash

backends_list_to_hash($c, $list)

returns array of backend ids converted as list of hashes

backends_hash_to_list

backends_hash_to_list($c, $hashlist)

returns array of backends (inverts backends_list_to_hash function)

parse_date_string

my $timestamp = parse_date_string($string)

Format: 2010-03-02 00:00:00 parse given date and return timestamp

expand_relative_timefilter

expand_relative_timefilter($string)

returns expanded timefilter value

convert_wildcards_to_regex

convert_wildcards_to_regex($string)

returns regular expression with wildcards replaced

find_modules

find_modules($pattern)

returns list of found modules

get_cli_modules

get_cli_modules()

returns list of cli modules

clean_regex

clean_regex()

returns cleaned regular expression, ex.: removes trailing .*

get_timezone_data

get_timezone_data()

returns list of available timezones

command_disabled

command_disabled($c, $nr)

returns true if command is disabled for current user

code2name

code2name($coderef)

returns name for given code reference

merge_service_dependencies

merge_service_dependencies($service, [$list, $list, ...])

merge parents, depends_exec and depends_notifiy lists into a single list

merge_host_dependencies

merge_host_dependencies([$list, $list, ...])

merge depends_exec and depends_notifiy into a single list

dump_params

dump_params($c->req->parameters, [$max_length], [$flat])

returns stringified parameters

dclone

dclone($obj)

deep clones any object

text_table

text_table( keys => [keys], data => <list of hashes>, [noheader => 1] )

a key can be:

    - "string"
    - ["column name", "data key"]
    - { "name" => "column header", "key" => "data key", type => $type, format => $formatstring }

returns ascii text table or undef on errors or no data/columns

get_expanded_start_date

get_expanded_start_date($c, $blocksize)

returns start of day for expanded duration

extract_time_filter

extract_time_filter($filter)

returns start and end time filter from given query

find_files

find_files(...)

alias for Thruk::Utils::IO::find_files

scale_out

scale_out( scale => worker_num, jobs => list of jobs, worker => sub ref, collect => sub ref )

scale out worker, run jobs and returns result from collect sub.

page_data

page_data($c, $data, [$result_size], [$total_size], [$already_paged])

adds paged data set to the template stash. Data will be available as 'data' The pager itself as 'pager'

render_db_profile

render_db_profile($c, $profiles)

return rendered db query profiles

has_node_module

has_node_module($c, $module_name)

return true if npm module is installed

extract_connection_error

extract_connection_error($err)

returns a short connection error along with the original error. Short error might be undef for unknown errors.

http_response_error

http_response_error($response)

returns a short connection error.

clean_old_folder_files

clean_old_folder_files($folder, $filter, $max_age)

removes all files from folder which are older than $max_age and match given $filter