Skip to content

Instantly share code, notes, and snippets.

@linuxmalaysia
Last active November 30, 2023 01:18
Show Gist options
  • Select an option

  • Save linuxmalaysia/ad31fe4dbfe76e9e8674d88ad9f0bcd5 to your computer and use it in GitHub Desktop.

Select an option

Save linuxmalaysia/ad31fe4dbfe76e9e8674d88ad9f0bcd5 to your computer and use it in GitHub Desktop.
this uptime.cron need to be in /etc/cron.d
# uptime report every 1 minutes everyday
*/10 * * * * root uptime | logger -t uptime > /dev/null
*/10 * * * * root free -h | head -2 | tail -1 | logger -t free > /dev/null
@linuxmalaysia

linuxmalaysia commented Aug 19, 2021

Copy link
Copy Markdown
Author
# For logstash elasticsearch

###  Start Rule uptime monitoring
### <13>Sep 13 00:23:01 myserver uptime:  00:23:01 up 8 days, 12:00,  3 users,  load average: 0.40, 0.38, 0.44
### after first clean

filter {

if "syslog" in [tags] and "uptime" in [program] {

grok {
  match => {
  "message" => [ "(?:%{TIMESTAMP_ISO8601:timestamp}|%{SYSLOGTIMESTAMP:timestamp})%{SPACE}%{HOSTNAME:host_target}%{SPACE}%{NOTSPACE}%{SPACE}%{NOTSPACE}%{SPACE}up%{SPACE}%{BASE10NUM:uptime_days}%{SPACE}%{NOTSPACE},%{SPACE}%{NOTSPACE:uptime_min},%{SPACE}%{BASE10NUM:users}%{SPACE}%{NOTSPACE}%{SPACE}load average:%{SPACE}%{NUMBER:load_1_minute},%{SPACE}%{NUMBER:load_5_minutes},%{SPACE}%{NUMBER:load_15_minutes}" ]
  }
  add_tag => [ "server_uptime", "monitoring" ]
  remove_field => [ "timestamp" ]
  }

 }


}

@linuxmalaysia

Copy link
Copy Markdown
Author

Suggestion from ChatGPT

Certainly! Your crontab entry looks good for monitoring uptime and free memory, but there are a couple of improvements that can be made for better readability and efficiency. Here's an updated version:

# Uptime report every 10 minutes every day
*/10 * * * * root /bin/bash -l -c 'uptime | logger -t uptime' > /dev/null

# Free memory report every 10 minutes every day
*/10 * * * * root /bin/bash -l -c 'free -h | awk "/Mem/{print}" | logger -t free' > /dev/null

Changes made:

  1. Added /bin/bash -l -c before the commands to ensure that the commands are executed in a login shell environment. This helps in loading the user's environment variables, which might be necessary for the proper execution of some commands.

  2. Used awk to filter the output of the free command to only include the line with "Mem". This makes the command more specific and avoids potential issues if the output format changes.

Feel free to adjust the frequency (*/10 * * * *) based on your monitoring needs. The provided version ensures that the commands are executed every 10 minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment