ギャバンITサービス
お菓子の家が作れるシステムエンジニアです

rtx1210でsyslog監視

rtx1210は高価だけどネットワークの勉強をするにはいい教材。 業務でも稀に箱モノの設定することがあるから、頭の体操にもなる。 ブラウザでログインしてポチポチ設定することから初め、設定ファイルを入れ込めば複雑なことができることもわかった。 コマンドラインでいろいろできることがわかったとき、設定ファイルがその積み上げ結果というのもわかった。 ブラウザから設定保存するとconfig0.txtっていうファイルが作成できる。 ログ監視 この設定ファイルに以下の内容を追記して、ログ監視してみる。 やってることはembedded fileっていうのを定義しておき、この内容をルータ起動時から実行させる。 ヤマハのページにsyslog監視のサンプルがあった。他にもいくつかググって方法を探し、試しながら作成。 検出したい文字列は、いったんlinuxの監視処理で使ってたものをそのまま流用。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 embedded file tmp.txt <<EOF --[[ ●SYSLOG 監視スクリプト-gvis SYSLOG を監視し、特定の文字列が含まれたログを検出したら、 指定したshow log コマンドの出力結果を管理者にメールで 送信するスクリプト。 メール本文には監視実施日の日付に検出された特定の文字列の履歴を含みます。 原本はyamahaさまの以下のページにあったものから改造作成し、 複数文字列を監視するようにしている。 https://network.yamaha.com/setting/router_firewall/monitor/lua_script/syslog ]] --------------------------## 設定値 ##-------------------------------- -- 検出したい SYSLOG の文字列パターン ptn_tbl = { -- ★ "error", "ERROR", "Error", "fail", "Fail", "FAIL", "panic", "Panic", "PANIC", "warn", "Warn", "WARN", "respond", "connect", "TUNNEL", "Login", "Logout", "Restarting router" } -- ptn の文字列パターンを検出したときに結果を通知する show コマンド cmd_whenFind = "show log reverse" -- ★ -- メールの設定 mail_tbl = { -- ★ smtp_address = "smtp.xxxx.jp", from = "xxxx@gavann-it.com", to = "xxxx@gavann-it.com" } -- メールの送信に失敗したときに出力する SYSLOG のレベル (info, debug, notice) log_level = "info" -- ★ ----------------------## 設定値ここまで ##---------------------------- ------------------------------------------------------------ -- コマンドを実行結果を出力する関数 -- ------------------------------------------------------------ function exec_command(cmd) local rtn, str rtn, str = rt.command(cmd) if (not rtn) or (not str) then str = "実行失敗\r\n" end return rtn, string.format("# %s\r\n%s\r\n", cmd, str) end ------------------------------------------------------------ -- 現在の日時を取得する関数 -- ------------------------------------------------------------ function time_stamp() local t t = os.date("*t") return string.format("%d/%02d/%02d %02d:%02d:%02d", t.year, t.month, t.day, t.hour, t.min, t.sec) end ------------------------------------------------------------ -- 今日の日付を取得する関数 -- ------------------------------------------------------------ function date_today() local today today = os.date("*t") return string.format("%d/%02d/%02d", today.year, today.month, today.day) end ------------------------------------------------------------ -- メインルーチン -- ------------------------------------------------------------ local rtn, logstr while (true) do -- SYSLOGから何か追加された文字列を格納するまで待つ rtn, logstr = rt.syslogwatch(".*") -- SYSLOG文字列が何か拾えたらマッチする文字列を探す if (rtn) and (logstr) then for i, str in ipairs(logstr) do for j, ptn in ipairs(ptn_tbl) do -- マッチする文字列があったらメールする if string.find(str, ptn) ~= nil then mail_tbl.text = string.format("ログに検索文字列が検出されました。\r\n検索文字列: \"%s\"\r\n\r\n", ptn) runCmd = cmd_whenFind .. " | grep " .. ptn .. " | grep " .. date_today() rtn, str = exec_command(runCmd) mail_tbl.text = mail_tbl.text .. str mail_tbl.subject = string.format("auto)alertReport-Netlog-rtx1210 (%s)", time_stamp()) rtn = rt.mail(mail_tbl) if (not rtn) then rt.syslog(log_level, "failed to send mail. (syslogWatch-rtx1210lua)") end end end end end end EOF embedded fileの定義を起動時から実行するように設定ファイルに書いておくと、SYSLOGを監視してくれる。 ...

 ⭐️

ルータのためのsyslog設定

ルータのログはそんなにたくさん残るものでもない。 再起動したら消える。 ではローカルのlinuxに残せないか。 ログを残したいというよりも、VPNしたりsshしたときに特定のキーワードがログに出力されるから、それを監視させたいというのが一番最初の目的。 windowsで syslogdもどき を動かすこともできるけど、シェルスクリプトで書いたログ監視をそのまま使いたかったので使わない。 ローカルlinuxで動いているrsyslogdがログを受け付けてくれる。 172.16.17.xxは水際のルータ。機種はrtx1210で外側は固定IP。一般家庭で使うルータが1万円かそこらで買えるけど、勉強のためもあって6万円ほどで購入。そのぶんできることも多く、プログラム必要だけどログの監視とメール送信ができたり、サポート外だけどGoogle CloudにVPN接続する設定まで入れて使ってる。 rtxシリーズのsyslog解説。 ローカルlinuxで受け付けるよう設定したのが以下。 centos7の場合 最初はcentos6の頃からやってたけど設定ファイル古すぎてバックアップ残してない。 centos8の頃に使ってた/etc/rsyslog.confの設定。 設定は「:fromhost-ip, isequal, “172.16.17.xx”」っていう記述を足してsyslogのデーモン再起動したはず。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 # rsyslog configuration file # For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html # If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html #### MODULES #### # The imjournal module bellow is now used as a message source instead of imuxsock. $ModLoad imuxsock # provides support for local system logging (e.g. via logger command) $ModLoad imjournal # provides access to the systemd journal #$ModLoad imklog # reads kernel messages (the same are read from journald) #$ModLoad immark # provides --MARK-- message capability # Provides UDP syslog reception #$ModLoad imudp #$UDPServerRun 514 # Provides TCP syslog reception #$ModLoad imtcp #$InputTCPServerRun 514 $ModLoad imudp $UDPServerRun 514 $umask 0022 $FileCreateMode 0644 $DirCreateMode 0755 #### GLOBAL DIRECTIVES #### # Where to place auxiliary files $WorkDirectory /var/lib/rsyslog # Use default timestamp format $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat # File syncing capability is disabled by default. This feature is usually not required, # not useful and an extreme performance hit #$ActionFileEnableSync on # Include all config files in /etc/rsyslog.d/ $IncludeConfig /etc/rsyslog.d/*.conf # Turn off message reception via local log socket; # local messages are retrieved through imjournal now. $OmitLocalLogging on # File to store the position in the journal $IMJournalStateFile imjournal.state #### RULES #### # Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.* /dev/console # Log anything (except mail) of level info or higher. # Don't log private authentication messages! *.info;mail.none;authpriv.none;cron.none;local0.none /var/log/messages # The authpriv file has restricted access. authpriv.* /var/log/secure # Log all the mail messages in one place. mail.* -/var/log/maillog # Log cron stuff cron.* /var/log/cron # Everybody gets emergency messages *.emerg :omusrmsg:* # Save news errors of level crit and higher in a special file. uucp,news.crit /var/log/spooler # Save boot messages also to boot.log local7.* /var/log/boot.log # Save network messages to network.log :fromhost-ip, isequal, "172.16.17.xx" /var/log/network_172.16.17.xx.log # ### begin forwarding rule ### # The statement between the begin ... end define a SINGLE forwarding # rule. They belong together, do NOT split them. If you create multiple # forwarding rules, duplicate the whole block! # Remote Logging (we use TCP for reliable delivery) # # An on-disk queue is created for this action. If the remote host is # down, messages are spooled to disk and sent when it is up again. #$ActionQueueFileName fwdRule1 # unique name prefix for spool files #$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible) #$ActionQueueSaveOnShutdown on # save messages to disk on shutdown #$ActionQueueType LinkedList # run asynchronously #$ActionResumeRetryCount -1 # infinite retries if host is down # remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional #*.* @@remote-host:514 # ### end of the forwarding rule ### ubuntu20の場合 無線LANの性能が低くなったなーと感じて買い換えたときに、syslogもいちおう残してみるかと思って設定追加。 ...

 ⭐️