3

mtr is a great tool for debugging the network packet losses. Here i sample mtr output. My traceroute [v0.85] myserver.com (0.0.0.0) Thu Jan 19 04:10:04 2017 Resolver: Received error response 2. (server failure)er of fields quit Packets Pings Host Loss% Snt Last Avg Best Wrst StDev 1. 192.168.104.23 0.0% 11 0.6 0.6 0.5 0.8 0.0 2. machine1.com 0.0% 11 8.5 12.4 2.0 20.5 5.5 3. mchine2.org.com 0.0% 11 1.2 1.0 0.8 1.8 0.0 4. machine3.orgcom 0.0% 11 0.8 0.9 0.7 1.1 0.0

However while running mtr on the server, you can't log-off the server.

I need mtr to output to a textfile and run in background similar to nohup command.

I should also be able to look into the report, something like using tail -f on the output file.

jophab
  • 5,356
  • 14
  • 41
  • 60
veer7
  • 20,074
  • 9
  • 46
  • 74

2 Answers2

4

mtr offers -r option, which puts mtr into report mode. In this mode, mtr will run for the number of cycles specified by the -c option then print statistics and exit. So we can create a script to run the command and put the script to cron entries on your schedule. For example:

/usr/sbin/mtr -r -c 2 www.google.com >> /home/mtr.log

Cron entry, run every minute:

* * * * * sh /path/to/script

Then you can tail -f on the output file.

jfly
  • 7,715
  • 3
  • 35
  • 65
0

If systemd is used

┌──[root@vms81.liruilongs.github.io]-[~]
└─$systemd-run --on-calendar=*:*:00  --unit mtr-print-log --slice mtr /usr/sbin/mtr -r -b  192.168.29.154
Running timer as unit mtr-print-log.timer.
Will run service as unit mtr-print-log.service.

Viewing mtr logs

┌──[root@vms81.liruilongs.github.io]-[~]
└─$journalctl -u mtr-print-log.service
-- Logs begin at 六 2022-12-24 21:56:02 CST, end at 六 2022-12-24 22:10:19 CST. --
12月 24 22:07:00 vms81.liruilongs.github.io systemd[1]: Started /usr/sbin/mtr -r -b 192.168.29.154.
12月 24 22:07:14 vms81.liruilongs.github.io mtr[15427]: Start: Sat Dec 24 22:07:00 2022
12月 24 22:07:14 vms81.liruilongs.github.io mtr[15427]: HOST: vms81.liruilongs.github.io  Loss%   Snt   Last   Avg  Best  Wrst StDev
12月 24 22:07:14 vms81.liruilongs.github.io mtr[15427]: 1.|-- gateway (192.168.26.2)     0.0%    10    0.4   0.3   0.2   0.5   0.0
12月 24 22:07:14 vms81.liruilongs.github.io mtr[15427]: 2.|-- 192.168.29.154             0.0%    10    1.5   0.9   0.7   1.5   0.0
12月 24 22:08:00 vms81.liruilongs.github.io systemd[1]: Started /usr/sbin/mtr -r -b 192.168.29.154.
12月 24 22:08:14 vms81.liruilongs.github.io mtr[16400]: Start: Sat Dec 24 22:08:00 2022
12月 24 22:08:14 vms81.liruilongs.github.io mtr[16400]: HOST: vms81.liruilongs.github.io  Loss%   Snt   Last   Avg  Best  Wrst StDev
12月 24 22:08:14 vms81.liruilongs.github.io mtr[16400]: 1.|-- gateway (192.168.26.2)     0.0%    10    0.3   0.3   0.2   0.4   0.0
12月 24 22:08:14 vms81.liruilongs.github.io mtr[16400]: 2.|-- 192.168.29.154             0.0%    10    1.0   1.0   0.7   1.4   0.0
12月 24 22:09:00 vms81.liruilongs.github.io systemd[1]: Started /usr/sbin/mtr -r -b 192.168.29.154.
12月 24 22:09:14 vms81.liruilongs.github.io mtr[17411]: Start: Sat Dec 24 22:09:00 2022
12月 24 22:09:14 vms81.liruilongs.github.io mtr[17411]: HOST: vms81.liruilongs.github.io  Loss%   Snt   Last   Avg  Best  Wrst StDev
12月 24 22:09:14 vms81.liruilongs.github.io mtr[17411]: 1.|-- gateway (192.168.26.2)     0.0%    10    0.3   0.3   0.3   0.5   0.0
12月 24 22:09:14 vms81.liruilongs.github.io mtr[17411]: 2.|-- 192.168.29.154             0.0%    10    0.9   0.9   0.7   1.3   0.0

If you only want to see the output and the execution time, you can.

┌──[root@vms81.liruilongs.github.io]-[~]
└─$journalctl -u  mtr-print-log.service -o cat | tail -n 10
Started /usr/sbin/mtr -r -b 192.168.29.154.
Start: Sat Dec 24 22:13:00 2022
HOST: vms81.liruilongs.github.io  Loss%   Snt   Last   Avg  Best  Wrst StDev
1.|-- gateway (192.168.26.2)     0.0%    10    0.2   0.3   0.2   0.5   0.0
2.|-- 192.168.29.154             0.0%    10    0.8   0.8   0.7   1.1   0.0
Started /usr/sbin/mtr -r -b 192.168.29.154.
Start: Sat Dec 24 22:14:00 2022
HOST: vms81.liruilongs.github.io  Loss%   Snt   Last   Avg  Best  Wrst StDev
1.|-- gateway (192.168.26.2)     0.0%    10    0.3   0.3   0.2   0.4   0.0
2.|-- 192.168.29.154             0.0%    10    0.9   0.8   0.7   1.0   0.0

Delete mtr process

┌──[root@vms81.liruilongs.github.io]-[~]
└─$systemctl stop mtr-print-log.timer
┌──[root@vms81.liruilongs.github.io]-[~]
└─$systemctl is-active mtr-print-log.service
unknown