LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-13-2022, 09:24 AM   #1
windowsuser
LQ Newbie
 
Registered: May 2021
Location: Poland
Distribution: Manjaro, Alpine, OpenWRT, Debian
Posts: 19

Rep: Reputation: Disabled
Trying to use crontab to make OpenWRT write persistent logs


I'm currently trying to use the following configuration, mostly through pipelines. This is to diagnose why the router sometimes breaks and requires a reboot which is quite frustrating due to my router's dual boot solution that leads to it falling back to the stock firmware after only one reboot on custom one, but requiring a triple reboot, which also requires waiting about 20 seconds before shutting down and then at least three before booting.
So the thing is, OpenWRT uses
Code:
logread
to display logs. But even after one attaches an external storage, the logs are(?) lost upon reboot. Therefore I came with a solution that utilizes the following crontab:
Code:
/*5     *       *       *       *       /root/loggen.sh                                                                                                                    
/*58    *       *       *       *       uniq -u /var/log/logfull | tee -a /var/log/logdump && rm -f /var/log/logfull
fwiw /var is symlinked to /tmp
The contents of the script:
Code:
if  [ ! -f /var/log/logfull ] ; then
        touch /var/log/logfull
else :
fi
logread | tail -n 30 | grep -Ev 'dnsmasq|simple-adblock' | tee -a /var/log/logfull
PS:
I know the uniq needs arguments or using cut in order to actually display unique entries that skip the timestamps at the start of each line. I've also found a solution that utilizes rev... but the packages providing it are nowhere to be found in OPKG repos... unless someone here knows if there is one?
 
Old 06-13-2022, 10:45 AM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
I've also found a solution that utilizes rev.
Is this what you are wanting?
Code:
echo "one two three" | rev
eerht owt eno
Quote:
but the packages providing it are nowhere to be found in OPKG repos... unless someone here knows if there is one?
Code:
for i in one two three; do
    reverse=("$i" "${reverse[@]}")
done

echo "${reverse[@]}"
three two one
Code:
#!/usr/bin/bash

input="One Two Three Four"
reverse=""
 
len=${#input}
for (( i=$len-1; i>=0; i-- )); do 
    reverse="$reverse${input:$i:1}"
done
 
echo "Original: "$input""
echo "Reversed: "$reverse""


Original: One Two Three Four
Reversed: ruoF eerhT owT enO

Last edited by teckk; 06-13-2022 at 10:47 AM.
 
Old 06-13-2022, 11:30 AM   #3
windowsuser
LQ Newbie
 
Registered: May 2021
Location: Poland
Distribution: Manjaro, Alpine, OpenWRT, Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
ok my attempt to tame my chaotic way of explaining stuff and also stick to UNIX KISS philosophy. Nevermind using rev, that was more of a side remark anyway.
I have the following pipe:
Code:
logread | cut -b 26-255 | uniq -u | tee -a /var/log/logfile
I have tried redirection and placing uniq at 1st and 2nd position, but it still keeps printing duplicate lines, like that:
Code:
 cron.err crond[2794]: user root: parse error at /*10
 cron.err crond[2794]: user root: parse error at /*5
 cron.err crond[2794]: user root: parse error at /*58
 kern.debug kernel: [106229.461647] ieee80211 phy0: Mac80211 start BA *****
 kern.debug kernel: [106231.344567] ieee80211 phy0: Stop BA ***
 cron.err crond[2794]: user root: parse error at /*10
 cron.err crond[2794]: user root: parse error at /*5
 cron.err crond[2794]: user root: parse error at /*58
Optimally I'd like to retain the timestamp, but only get the first occurence of an unique error in a nice, decluttered log file, I assume using uniq -s 5 and changing to cut -b 12-16,25-255 would be enough?

Last edited by windowsuser; 06-13-2022 at 11:31 AM.
 
Old 06-13-2022, 12:04 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Do you have a sample of logread | tail -n 30 that you can post? So that someone can see what you are trying to achieve.

Quote:
/root/loggen.sh
Don't usually put scripts in /root directory.

So far all that we can see is that you are trying to parse a logfile.

Show a log sample, and what fields that you wish to keep. Could be that awk will spit that out with one line.

Or perhaps a python parsing script.

Last edited by teckk; 06-13-2022 at 12:05 PM.
 
Old 06-13-2022, 12:42 PM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
Optimally I'd like to retain the timestamp, but only get the first occurence of an unique error in a nice
Here is an example if it helps.
Code:
a="1 1 3 3 4 4 7 7 7 8 9 10 11 12 13 13 13 14"

IFS=' '
for i in $a; do
  num1+=( [$i]=" " )
done

echo ${!num1[@]} | sort
1 3 4 7 8 9 10 11 12 13 14


b=(1 1 3 3 4 4 7 7 7 8 9 10 11 12 13 13 13 14)

IFS=' '
for i in "${b[@]}"; do
  num2+=( [$i]=" " )
done

echo ${!num2[@]} | sort
1 3 4 7 8 9 10 11 12 13 14
 
Old 06-15-2022, 06:37 PM   #6
windowsuser
LQ Newbie
 
Registered: May 2021
Location: Poland
Distribution: Manjaro, Alpine, OpenWRT, Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Do you have a sample of logread | tail -n 30 that you can post? So that someone can see what you are trying to achieve.
This is what it looks like. I want to filter out repetitive errors stemming from stuff I just haven't managed to find enough time for and make me able to search for important critical errors that could help me figure out why the router keeps crashing, why it drops 2.4 GHz radio etc. Because normally this log gets cleared once the router reboots and I want to write them into a file on the 64 GB pendrive (enough to write a really big, dirty log file, but I also want it to be easily legible for obvious reasons and not trash the router's ram once opened) to which some of the sysdirs are mounted, incl. /var/log. Sensitive data such as MACs is censored with (...):

Code:
Wed Jun 15 22:34:45 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:35:01 2022 kern.debug kernel: [304285.120658] ieee80211 phy0: Mac80211 start BA (...)
Wed Jun 15 22:38:05 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:41:26 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:44:47 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:48:08 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:51:29 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:54:50 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:58:11 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 23:01:32 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 23:04:53 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 23:08:14 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 23:08:17 2022 daemon.notice hostapd: wlan0: AP-STA-DISCONNECTED (...)
Wed Jun 15 23:08:17 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: disassociated
Wed Jun 15 23:08:18 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: deauthenticated due to inactivity (timer DEAUTH/REMOVE)
Wed Jun 15 23:19:00 2022 cron.err crond[2794]: user root: parse error at /*10
Wed Jun 15 23:19:00 2022 cron.err crond[2794]: user root: parse error at /*5
Wed Jun 15 23:19:00 2022 cron.err crond[2794]: user root: parse error at /*58
Wed Jun 15 23:21:11 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 23:21:25 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: authenticated
Wed Jun 15 23:21:25 2022 daemon.notice hostapd: wlan0: STA-OPMODE-N_SS-CHANGED (...)
Wed Jun 15 23:21:25 2022 daemon.info hostapd: wlan0: STA (...) IEEE 802.11: associated (aid 1)
Wed Jun 15 23:21:25 2022 daemon.notice hostapd: wlan0: AP-STA-CONNECTED (...)
Wed Jun 15 23:21:25 2022 daemon.info hostapd: wlan0: STA (...) WPA: pairwise key handshake completed (RSN)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPDISCOVER(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPOFFER(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPREQUEST(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:27 2022 daemon.info dnsmasq-dhcp[5019]: DHCPACK(br-lan) 192.168.1.(...)
Wed Jun 15 23:21:40 2022 authpriv.info dropbear[3194]: Child connection from 192.168.1.(...)
Wed Jun 15 23:21:40 2022 authpriv.notice dropbear[3194]: Pubkey auth succeeded for 'root' with key sha1!! (...) from 192.168.1.(...)
So for example I want to show repetitive errors once or if possible twice – first and most recent occurence, with a timestamp. The rest needs to be filtered out. Some, like the bandwidthd error might also be trunctated, but that's risky. The most primitive way of achieving what I need that I think of is of course with grep -v, but I want something more neat.

Last edited by windowsuser; 06-15-2022 at 06:41 PM.
 
Old 06-16-2022, 09:35 AM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
So for example I want to show repetitive errors once or if possible twice
Code:
txt="
Wed Jun 15 22:38:05 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:41:26 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:44:47 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:48:08 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
Wed Jun 15 22:51:29 2022 daemon.err bandwidthd: Connection to database 'user = postgres dbname = bandwidthd host = 192.168.1.1' failed: could not connect to server: Connection refused 	Is the server running on host "192.168.1.1" and accepting 	TCP/IP connections on port (...)
"

grep -nFx "$(sort <<< "$txt" | uniq -d)" <<< "$txt" 
1:
7:
Only lines 1 and 7 match. Because they are empty lines. All the rest are different.(Time stamps)

So, get rid of the time stamps and compare the rest.
Example:
Code:
while read line; do 
    [[ "$line" =~ 2022(.*) ]] && echo "${BASH_REMATCH[1]}"
done <<< "$txt"
 
1 members found this post helpful.
Old 06-16-2022, 10:26 AM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Give you a couple more examples for you to reference while building your script.

Code:
txt="
Wed Jun 15 22:38:05 2022 Mary had a little lamb
Wed Jun 15 22:38:06 2022 Mary had a little lamb
Wed Jun 15 22:41:26 2022 Whose fleece was white as snow
Wed Jun 15 22:41:27 2022 Whose fleece was white as snow
Wed Jun 15 22:44:47 2022 And everywhere that Mary went
Wed Jun 15 22:44:48 2022 And everywhere that Mary went
Wed Jun 15 22:44:49 2022 And everywhere that Mary went
Wed Jun 15 22:48:08 2022 The lamb was sure to go
Wed Jun 15 22:51:29 2022 The end
Wed Jun 15 22:51:30 2022 The end
Wed Jun 15 22:51:31 2022 The end
Wed Jun 15 22:51:32 2022 The end
"

while read line; do 
    [[ "$line" =~ 2022(.*) ]] && echo "${BASH_REMATCH[1]}"
done <<< "$txt" | uniq

while read line; do
    echo "${line%%2022 *}"
done <<< "$txt"
 
  


Reply

Tags
crontab, diagnostics, logs, openwrt, pipeline



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make custom crontab entry persistent dr-artsi Linux - Newbie 8 10-07-2018 01:44 AM
Back up logs file and create a script showing the backed up logs and the running logs Billy_6052 Programming 5 12-13-2014 02:32 AM
Persistent persistent Persistent Going Nuts Here Fcukinyahoo Linux - Newbie 6 11-17-2011 09:56 PM
Crontab in OpenWRT offworld21 Linux - General 5 09-05-2006 02:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration