| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Rex::Commands::Sysctl - Manipulate sysctl | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | With this module you can set and get sysctl parameters. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Version <= 1.0: All these functions will not be reported. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | All these functions are not idempotent. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use Rex::Commands::Sysctl; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my $data = sysctl "net.ipv4.tcp_keepalive_time"; | ||||||||||||||||||||||||||||
| 22 | sysctl "net.ipv4.tcp_keepalive_time" => 1800; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | package Rex::Commands::Sysctl; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 37 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 37 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 37 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 37 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 37 | ▶ | use Rex::Commands::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 37 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 37 | ▶ | use Rex::Commands::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 37 | require Rex::Exporter; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 37 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 37 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 37 | @EXPORT = qw(sysctl); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head2 sysctl($key [, $val [, %options]]) | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | This function will read the sysctl key $key. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | If $val is given, then this function will set the sysctl key $key. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | task "tune", "server01", sub { | ||||||||||||||||||||||||||||
| 54 | if( sysctl("net.ipv4.ip_forward") == 0 ) { | ||||||||||||||||||||||||||||
| 55 | sysctl "net.ipv4.ip_forward" => 1; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | If both $val and ensure option are used, the sysctl key is modified and the value may persist in /etc/sysctl.conf depending if ensure option is "present" or "absent". | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | With ensure => "present", if the key already exists in the file, it will be updated to the new value. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | task "forwarding", "server01", sub { | ||||||||||||||||||||||||||||
| 64 | sysctl "net.ipv4.ip_forward" => 1, ensure => "present"; | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub sysctl_save { | ||||||||||||||||||||||||||
|
Subroutine
sysctl_save: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ( $key, $value ) = @_; | |||||||||||||||||||||||||||
| 71 | 0 | my $sysctl = get_sysctl_command(); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | append_or_amend_line "/etc/sysctl.conf", | ||||||||||||||||||||||||||||
| 74 | line => "$key=$value", | ||||||||||||||||||||||||||||
| 75 | regexp => qr{\Q$key=}, | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | on_change => sub { i_run "$sysctl -p" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub sysctl_remove { | ||||||||||||||||||||||||||
|
Subroutine
sysctl_remove: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 0 | my ( $key, $value ) = @_; | |||||||||||||||||||||||||||
| 81 | 0 | my $sysctl = get_sysctl_command(); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | delete_lines_according_to "$key=$value", "/etc/sysctl.conf", | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | on_change => sub { i_run "$sysctl -p" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub sysctl { | ||||||||||||||||||||||||||
|
Subroutine
sysctl: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my ( $key, $val, %options ) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | my $sysctl = get_sysctl_command(); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( defined $val ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | Rex::Logger::debug("Setting sysctl key $key to $val"); | |||||||||||||||||||||||||||
| 95 | 0 | my $ret = i_run "$sysctl -n $key"; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $ret ne $val ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | i_run "$sysctl -w $key=$val", fail_ok => 1; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | die("Sysctl failed $key -> $val"); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 0 | Rex::Logger::debug("$key has already value $val"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( $options{ensure} || $options{persistent} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$options{"ensure"} or $options{"persistent"}
MC/DC0%0/2
$options{"ensure"} or $options{"persistent"}
$options{"ensure"}
$options{"persistent"}
Truth table0%0/3
A or B
A= $options{"ensure"}B= $options{"persistent"}
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( $options{ensure} eq "present" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | 0 | Rex::Logger::debug("Writing $key=$val to sysctl.conf"); | |||||||||||||||||||||||||||
| 110 | 0 | sysctl_save $key, $val; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | elsif ( $options{ensure} eq "absent" ) { | ||||||||||||||||||||||||||||
| 113 | 0 | Rex::Logger::debug("Removing $key=$val of sysctl.conf"); | |||||||||||||||||||||||||||
| 114 | 0 | sysctl_remove $key, $val; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||
| 117 | Rex::Logger::info( | ||||||||||||||||||||||||||||
| 118 | 0 | "Error : " . $options{ensure} . " is not a known ensure parameter" ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my $ret = i_run "$sysctl -n $key", fail_ok => 1; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ( $? == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | return $ret; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | 0 | Rex::Logger::info( "Error getting sysctl key: $key", "warn" ); | |||||||||||||||||||||||||||
| 131 | 0 | die("Error getting sysctl key: $key"); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub get_sysctl_command { | ||||||||||||||||||||||||||
|
Subroutine
get_sysctl_command: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 0 | my $sysctl = can_run( '/sbin/sysctl', '/usr/sbin/sysctl' ); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( !defined $sysctl ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | my $message = q(Couldn't find sysctl executable); | |||||||||||||||||||||||||||
| 143 | 0 | Rex::Logger::info( $message, 'error' ); | |||||||||||||||||||||||||||
| 144 | 0 | die($message); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | return $sysctl; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 37 | 1; | |||||||||||||||||||||||||||