| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # server::syslogng Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Server::Syslogng; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Shell::Command Metabrik::System::Package); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable syslog log logging syslog-ng) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | datadir => [ qw(datadir) ], | ||
| 20 | output => [ qw(file) ], | ||
| 21 | listen => [ qw(address) ], | ||
| 22 | port => [ qw(port) ], | ||
| 23 | conf_file => [ qw(file) ], | ||
| 24 | to_remote => [ qw(host) ], | ||
| 25 | to_port => [ qw(port) ], | ||
| 26 | use_ssl => [ qw(0|1) ], | ||
| 27 | ca_dir => [ qw(directory) ], | ||
| 28 | key_file => [ qw(file) ], | ||
| 29 | cert_file => [ qw(file) ], | ||
| 30 | version => [ qw(version) ], | ||
| 31 | }, | ||
| 32 | attributes_default => { | ||
| 33 | listen => '127.0.0.1', | ||
| 34 | port => 6300, | ||
| 35 | output => 'local.log', | ||
| 36 | use_ssl => 0, | ||
| 37 | version => '3.5', | ||
| 38 | }, | ||
| 39 | commands => { | ||
| 40 | install => [ ], # Inherited | ||
| 41 | generate_conf => [ qw(conf_file|OPTIONAL) ], | ||
| 42 | start => [ qw(conf_file|OPTIONAL) ], | ||
| 43 | stop => [ qw(pidfile|OPTIONAL) ], | ||
| 44 | }, | ||
| 45 | require_modules => { | ||
| 46 | 'Metabrik::File::Text' => [ ], | ||
| 47 | 'Metabrik::System::File' => [ ], | ||
| 48 | }, | ||
| 49 | require_binaries => { | ||
| 50 | 'syslog-ng' => [ ], | ||
| 51 | }, | ||
| 52 | need_packages => { | ||
| 53 | ubuntu => [ qw(syslog-ng) ], | ||
| 54 | debian => [ qw(syslog-ng) ], | ||
| 55 | kali => [ qw(syslog-ng) ], | ||
| 56 | }, | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub brik_use_properties { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $datadir = $self->datadir; | ||
| 64 | |||
| 65 | return { | ||
| 66 | attributes_default => { | ||
| 67 | conf_file => "$datadir/syslogng.conf", | ||
| 68 | }, | ||
| 69 | }; | ||
| 70 | } | ||
| 71 | |||
| 72 | # | ||
| 73 | # https://www.balabit.com/documents/syslog-ng-ose-3.5-guides/en/syslog-ng-ose-guide-admin/html-single/index.html | ||
| 74 | # | ||
| 75 | |||
| 76 | sub generate_conf { | ||
| 77 | my $self = shift; | ||
| 78 | my ($conf_file) = @_; | ||
| 79 | |||
| 80 | $conf_file ||= $self->conf_file; | ||
| 81 | |||
| 82 | my $datadir = $self->datadir; | ||
| 83 | my $user = defined($self->global) && $self->global->username || 'username'; | ||
| 84 | my $hostname = defined($self->global) && $self->global->hostname || 'hostname'; | ||
| 85 | my $group = $user; | ||
| 86 | my $listen = $self->listen; | ||
| 87 | my $port = $self->port; | ||
| 88 | my $output = $self->output; | ||
| 89 | my $remote_host = $self->to_remote; | ||
| 90 | my $remote_port = $self->to_port; | ||
| 91 | my $use_ssl = $self->use_ssl; | ||
| 92 | my $ca_dir = $self->ca_dir; | ||
| 93 | my $key_file = $self->key_file; | ||
| 94 | my $cert_file = $self->cert_file; | ||
| 95 | my $version = $self->version; | ||
| 96 | |||
| 97 | my $sf = Metabrik::System::File->new_from_brik_init($self) or return; | ||
| 98 | if ($sf->is_relative($output)) { | ||
| 99 | $output = "$datadir/$output"; | ||
| 100 | } | ||
| 101 | |||
| 102 | my $conf = '@version:'."$version\n"; | ||
| 103 | if (-f '/etc/syslog-ng/scl.conf') { | ||
| 104 | $conf .= '@include "scl.conf"'."\n"; | ||
| 105 | } | ||
| 106 | $conf .= "\n"; | ||
| 107 | |||
| 108 | $conf .=<<EOF | ||
| 109 | options { | ||
| 110 | use-dns(no); | ||
| 111 | use-fqdn(no); | ||
| 112 | keep-hostname(yes); | ||
| 113 | chain-hostnames(no); | ||
| 114 | owner("$user"); | ||
| 115 | group("$group"); | ||
| 116 | perm(0644); | ||
| 117 | stats-freq(120); | ||
| 118 | |||
| 119 | # Performance optimizations | ||
| 120 | # From: https://pzolee.blogs.balabit.com/2011/02/syslog-ng-performance-tuning/ | ||
| 121 | flush-lines(100); | ||
| 122 | log-fifo-size(1000); | ||
| 123 | }; | ||
| 124 | |||
| 125 | source s_internal { | ||
| 126 | internal(); | ||
| 127 | }; | ||
| 128 | |||
| 129 | destination d_local_syslogng { | ||
| 130 | file("$datadir/syslogng.log"); | ||
| 131 | }; | ||
| 132 | |||
| 133 | log { source(s_internal); destination(d_local_syslogng); }; | ||
| 134 | |||
| 135 | source s_listen_udp { | ||
| 136 | udp(ip($listen) port($port) | ||
| 137 | host-override("$hostname") | ||
| 138 | log-iw-size(100) | ||
| 139 | log-fetch-limit(100) | ||
| 140 | ); | ||
| 141 | }; | ||
| 142 | |||
| 143 | destination d_local_file { | ||
| 144 | file("$output"); | ||
| 145 | }; | ||
| 146 | |||
| 147 | EOF | ||
| 148 | ; | ||
| 149 | |||
| 150 | if (defined($remote_host) && $use_ssl) { | ||
| 151 | $conf .=<<EOF | ||
| 152 | destination d_remote_host { | ||
| 153 | tcp("$remote_host" port($remote_port) | ||
| 154 | tls( | ||
| 155 | ca-dir("$ca_dir") | ||
| 156 | key-file("$key_file") | ||
| 157 | cert-file("$cert_file") | ||
| 158 | ) | ||
| 159 | ); | ||
| 160 | }; | ||
| 161 | |||
| 162 | log { source(s_listen_udp); destination(d_remote_host); flags(flow-control); }; | ||
| 163 | EOF | ||
| 164 | ; | ||
| 165 | } | ||
| 166 | elsif (defined($remote_host)) { | ||
| 167 | $conf .=<<EOF | ||
| 168 | destination d_remote_host { tcp("$remote_host" port($remote_port) ); }; | ||
| 169 | |||
| 170 | log { source(s_listen_udp); destination(d_remote_host); flags(flow-control); }; | ||
| 171 | EOF | ||
| 172 | ; | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | $conf .=<<EOF | ||
| 176 | log { source(s_listen_udp); destination(d_local_file); }; | ||
| 177 | EOF | ||
| 178 | ; | ||
| 179 | } | ||
| 180 | |||
| 181 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 182 | $ft->append(0); | ||
| 183 | $ft->overwrite(1); | ||
| 184 | $ft->write($conf, $conf_file) or return; | ||
| 185 | |||
| 186 | return $conf_file; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub start { | ||
| 190 | my $self = shift; | ||
| 191 | my ($conf_file) = @_; | ||
| 192 | |||
| 193 | $conf_file ||= $self->conf_file; | ||
| 194 | $self->brik_help_run_undef_arg('start', $conf_file) or return; | ||
| 195 | |||
| 196 | my $datadir = $self->datadir; | ||
| 197 | $self->brik_help_run_file_not_found('start', $conf_file) or return; | ||
| 198 | |||
| 199 | my $ctlfile = $datadir.'/syslogng.ctl'; | ||
| 200 | my $persistfile = $datadir.'/syslogng.persist'; | ||
| 201 | my $pidfile = $datadir.'/syslogng.pidfile'; | ||
| 202 | |||
| 203 | if (-f $pidfile) { | ||
| 204 | return $self->log->error("start: syslogng already started with pidfile [$pidfile]"); | ||
| 205 | } | ||
| 206 | |||
| 207 | my $cmd = "syslog-ng -f \"$conf_file\" -c \"$ctlfile\" -R \"$persistfile\" --pidfile \"$pidfile\""; | ||
| 208 | $self->ignore_error(0); | ||
| 209 | my $r = $self->system($cmd) or return; | ||
| 210 | if ($r == 256) { | ||
| 211 | return $self->log->error("start: unable to start syslogng: code [$r]"); | ||
| 212 | } | ||
| 213 | elsif ($r > 0) { | ||
| 214 | $self->log->warning("start: some errors found while starting syslogng: code [$r]"); | ||
| 215 | } | ||
| 216 | |||
| 217 | return $pidfile; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub stop { | ||
| 221 | my $self = shift; | ||
| 222 | my ($pidfile) = @_; | ||
| 223 | |||
| 224 | if (! defined($pidfile)) { | ||
| 225 | my $datadir = $self->datadir; | ||
| 226 | $pidfile = $datadir.'/syslogng.pidfile'; | ||
| 227 | } | ||
| 228 | if (! -f $pidfile) { | ||
| 229 | return $self->log->error("start: syslogng NOT started with pidfile [$pidfile]"); | ||
| 230 | } | ||
| 231 | |||
| 232 | my $sp = Metabrik::System::Process->new_from_brik_init($self) or return; | ||
| 233 | return $sp->kill_from_pidfile($pidfile); | ||
| 234 | } | ||
| 235 | |||
| 236 | 1; | ||
| 237 | |||
| 238 | __END__ | ||