Commit 5ff378c9 authored by Luc Didry's avatar Luc Didry

Initial commit - working

parents
Pipeline #3569 skipped
This diff is collapsed.
# Dynamic motd
The aim of this project is to give some informations when you log into a server through SSH.
Example:
```
___ ___ _ ____ _____ _ __
/ __|/ _ \ '__\ \ / / _ \ '__|
\__ \ __/ | \ V / __/ |
|___/\___|_| \_/ \___|_|
Debian GNU/Linux 8.4 (jessie) (kernel 3.16.0-4-amd64)
System information as of Fri Apr 22 10:02:57 2016
System load: 2.74 Processes: 167
Memory usage: 98% Users logged in: 1
Swap usage: 31%
Usage of / : 40.6% of 23.71GB
Usage of /boot : 17.0% of 0.44GB
Usage of /mnt : 73.2% of 1863.01GB
Logged in users:
user from laptop.example.org at Fri Apr 22 09:09:09 2016
No mail.
Last login: Fri Apr 22 09:23:01 2016 from laptop.example.org
```
**Warning** This Debian and Debian-related distributions only.
## Dependencies
You need to install some packages:
```
apt-get install figlet lsb-release python-utmp bc
```
Optionnally, you can install `needrestart` which is used to show a message if your server need a reboot (main reason (and the only one I know): you have upgraded your kernel).
If you don't install `needrestart`, it will work, but you won't be warned about the need for a reboot.
## Installation
```
cp -r update-motd.d/ /etc
rm /etc/motd
ln -s /var/run/motd /etc/motd
```
## Salt
You will find a working salt formula in `init.sls`.
```
cd /srv/salt
git clone https://framagit.org/luc/dynamic-motd.git motd
salt your_server state.sls motd
```
## License
GPLv2. Have a look at the [LICENSE file](LICENSE).
## Acknowledments
- Dustin Kirkland, the guy behind the Ubuntu dynamic motd (I took some scripts from Ubuntu and stole inspiration too :D)
- https://github.com/maxis1718/update-motd.d for the skeleton
- https://github.com/jnweiger/landscape-sysinfo-mini for the python script (slightly modified)
motd-packages:
pkg.installed:
- pkgs:
- figlet
- lsb-release
- python-utmp
- needrestart
- bc
symbolic-motd:
file.symlink:
- name: /etc/motd
- target: /var/run/motd
- force: True
- backupname: Truek
dynamic-motd:
file.recurse:
- name: /etc/update-motd.d/
- source: salt://motd/update-motd.d/
- file_mode: 755
remove-exec-colors:
file.managed:
- name: /etc/update-motd.d/colors
- mode: 644
remove-exec-sysinfo:
file.managed:
- name: /etc/update-motd.d/sysinfo.py
- mode: 644
#!/bin/sh
. /etc/update-motd.d/colors
printf "\n"$LIGHT_RED
figlet " "$(hostname -s)
printf $NONE
printf "\n"
#!/bin/bash
#
# 00-header - create the header of the MOTD
# Copyright (C) 2009-2010 Canonical Ltd.
#
# Authors: Dustin Kirkland <kirkland@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Modified by Luc Didry in 2016
. /etc/update-motd.d/colors
[ -r /etc/update-motd.d/lsb-release ] && . /etc/update-motd.d/lsb-release
if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
# Fall back to using the very slow lsb_release utility
DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi
re='(.*\()(.*)(\).*)'
if [[ $DISTRIB_DESCRIPTION =~ $re ]]; then
DISTRIB_DESCRIPTION=$(printf "%s%s%s%s%s" "${BASH_REMATCH[1]}" "${YELLOW}" "${BASH_REMATCH[2]}" "${NONE}" "${BASH_REMATCH[3]}")
fi
echo -e " "$DISTRIB_DESCRIPTION "(kernel "$(uname -r)")\n"
# Update the information for next time
printf "DISTRIB_DESCRIPTION=%s" $(lsb_release -s -d) > /etc/update-motd.d/lsb-release &
#!/bin/sh
. /etc/update-motd.d/colors
cores=$(grep -c ^processor /proc/cpuinfo 2>/dev/null)
[ "$cores" -eq "0" ] && cores=1
threshold="${cores:-1}.0"
echo
if [ $(echo "`cut -f1 -d ' ' /proc/loadavg` < $threshold" | bc) -eq 1 ]; then
python /etc/update-motd.d/sysinfo.py
else
echo " System information disabled due to load higher than $threshold"
fi
printf "\n"
#!/bin/bash
. /etc/update-motd.d/colors
n=$(apt-get -qq --just-print upgrade | cut -f 2 -d " " | sort -u | wc -l)
if [[ $n -gt 0 ]]; then
printf $LIGHT_RED
printf " You have %s packages waiting for upgrades." "$n"
printf $NONE"\n\n"
fi
#!/bin/bash
. /etc/update-motd.d/colors
if [[ -x /usr/sbin/needrestart ]]; then
n=$(DEBIAN_FRONTEND=noninteractive echo "" | needrestart -k -v -n 2>/dev/null | grep -c "Pending kernel upgrade\!")
if [[ $n -gt 0 ]]; then
printf $LIGHT_RED
printf " Pending kernel upgrade! "
printf $NONE
printf "You should consider rebooting your machine.\n\n"
fi
fi
NONE="\033[m"
WHITE="\033[1;37m"
GREEN="\033[1;32m"
RED="\033[0;32;31m"
YELLOW="\033[1;33m"
BLUE="\033[34m"
CYAN="\033[36m"
LIGHT_GREEN="\033[1;32m"
LIGHT_RED="\033[1;31m"
#!/usr/bin/python
#
# landscape-sysinfo-mini.py -- a trivial re-implementation of the
# sysinfo printout shown on debian at boot time. No twisted, no reactor, just /proc & utmp
#
# (C) 2014 jw@owncloud.com
#
# inspired by ubuntu 14.10 /etc/update-motd.d/50-landscape-sysinfo
# Requires: python-utmp
# for counting users.
#
# 2014-09-07 V1.0 jw, ad hoc writeup, feature-complete. Probably buggy?
# 2014-10-08 V1.1 jw, survive without swap
# 2014-10-13 V1.2 jw, survive without network
# Modified by Luc Didry in 2016
# Get the original version at https://github.com/jnweiger/landscape-sysinfo-mini
import sys,os,time,posix,glob,utmp
from UTMPCONST import *
_version_ = '1.2'
def utmp_count():
u = utmp.UtmpRecord()
users = 0
for i in u:
if i.ut_type == utmp.USER_PROCESS: users += 1
return users
def proc_meminfo():
items = {}
for l in open('/proc/meminfo').readlines():
a = l.split()
items[a[0]] = int(a[1])
# print items['MemTotal:'], items['MemFree:'], items['SwapTotal:'], items['SwapFree:']
return items
def proc_mount():
items = {}
for m in open('/proc/mounts').readlines():
a = m.split()
if a[0].find('/dev/') is 0:
statfs = os.statvfs(a[1])
perc = 100-100.*statfs.f_bavail/statfs.f_blocks
gb = statfs.f_bsize*statfs.f_blocks/1024./1024/1024
items[a[1]] = "%.1f%% of %.2fGB" % (perc, gb)
return items
loadav = float(open("/proc/loadavg").read().split()[1])
processes = len(glob.glob('/proc/[0-9]*'))
statfs = proc_mount()
users = utmp_count()
meminfo = proc_meminfo()
memperc = "%d%%" % (100-100.*meminfo['MemFree:']/(meminfo['MemTotal:'] or 1))
swapperc = "%d%%" % (100-100.*meminfo['SwapFree:']/(meminfo['SwapTotal:'] or 1))
if meminfo['SwapTotal:'] == 0: swapperc = '---'
print " System information as of %s\n" % time.asctime()
print " System load: %-5.2f Processes: %d" % (loadav, processes)
print " Memory usage: %-4s Users logged in: %d" % (memperc, users)
print " Swap usage: %s" % (swapperc)
for k in sorted(statfs.keys()):
print " Usage of %-24s: %-20s" % (k, statfs[k])
if users > 0:
a = utmp.UtmpRecord()
print "\n Logged in users:"
for b in a: # example of using an iterator
if b.ut_type == USER_PROCESS:
print " \033[1;31m%-10s\033[m from %-25s at %-20s" % \
(b.ut_user, b.ut_host, time.ctime(b.ut_tv[0]))
a.endutent()
sys.exit(0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment