diff options
author | Ting-Wei Lan <lantw44@gmail.com> | 2015-01-29 21:19:32 +0800 |
---|---|---|
committer | Ting-Wei Lan <lantw44@gmail.com> | 2015-01-29 21:19:32 +0800 |
commit | ae8bb35d10c22e3c62b963b5b7f70b4475fd9b6f (patch) | |
tree | 70d5ec6f878dc63856a26e9788828409d283e320 /admin | |
download | misc-tools-ae8bb35d10c22e3c62b963b5b7f70b4475fd9b6f.tar.gz misc-tools-ae8bb35d10c22e3c62b963b5b7f70b4475fd9b6f.tar.zst misc-tools-ae8bb35d10c22e3c62b963b5b7f70b4475fd9b6f.zip |
Initial commit - import things written for various hosts
Diffstat (limited to 'admin')
-rwxr-xr-x | admin/rpmdu.sh | 50 | ||||
-rwxr-xr-x | admin/rpmsize.sh | 43 | ||||
-rwxr-xr-x | admin/search-login.py | 65 | ||||
-rwxr-xr-x | admin/ssh-bad-login.sh | 39 | ||||
-rwxr-xr-x | admin/yumdb-reason-editor.py | 198 |
5 files changed, 395 insertions, 0 deletions
diff --git a/admin/rpmdu.sh b/admin/rpmdu.sh new file mode 100755 index 0000000..ef360b1 --- /dev/null +++ b/admin/rpmdu.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +div_base=1024 +div_name="KB" +if [ "`echo "$1" | cut -c 1`" = "-" ] +then + optname=`echo "$1" | cut -c 2-` + case "$optname" in + "k") + ;; + "m") + div_base=1048576 + div_name=MB + ;; + "g") + div_base=1073741824 + div_name=GB + ;; + *) + echo "Usage: `basename "$0"` [OPTION] package_name..." + echo " -k KB" + echo " -m MB" + echo " -g GB" + exit 1 + ;; + esac + shift +fi + +while [ "$1" ] +do + rpm -ql "$1" | { + while read oneline + do + if [ -f "$oneline" ] + then + du -k "$oneline" + fi + done + } | { + while read -d $'\t' filesize + do + total=$((${total}+${filesize})) + read + done + printf "%9d %s %s\n" "$(($total/$div_base))" "$div_name" "$1" + } + + shift +done + diff --git a/admin/rpmsize.sh b/admin/rpmsize.sh new file mode 100755 index 0000000..d884810 --- /dev/null +++ b/admin/rpmsize.sh @@ -0,0 +1,43 @@ +#!/bin/sh +div_base=1 +div_name="Bytes" +if [ "`echo "$1" | cut -c 1`" = "-" ] +then + optname=`echo "$1" | cut -c 2-` + case "$optname" in + "b") + ;; + "k") + div_base=1024 + div_name=KB + ;; + "m") + div_base=1048576 + div_name=MB + ;; + "g") + div_base=1073741824 + div_name=GB + ;; + *) + echo "Usage: `basename "$0"` [OPTION] package_name..." + echo " -b Byte" + echo " -k KB" + echo " -m MB" + echo " -g GB" + exit 1 + ;; + esac + shift +fi +while [ "$1" ] +do + total=0 + filesize=`rpm -q "$1" --queryformat "%{SIZE} "` + for i in $filesize + do + total=$((${total}+${i})) + done + printf "%12d %s %s\n" "$(($total/$div_base))" "$div_name" "$1" + shift +done diff --git a/admin/search-login.py b/admin/search-login.py new file mode 100755 index 0000000..ba87958 --- /dev/null +++ b/admin/search-login.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import sys +import re +from datetime import datetime + +if len(sys.argv) < 3: + sys.stderr.write( + 'Usage: {} output_of_last_FRw event_list\n'.format(sys.argv[0])) + quit(1) + +last_output = open(sys.argv[1], 'r') +event_list = open(sys.argv[2], 'r') +records = [] + +while True: + last_line_raw = last_output.readline().strip() + if not last_line_raw: + break + + last_line = last_line_raw + user_end_index = last_line.find(' ') + user = last_line[0:user_end_index] + if user == 'reboot': + continue + + last_line = last_line[user_end_index:len(last_line)].strip() + terminal_end_index = last_line.find(' ') + terminal = last_line[0:terminal_end_index] + + day_start_index = last_line.find(' ') + last_line = last_line[day_start_index:len(last_line)].strip() + date_start_index = last_line.find(' ') + last_line = last_line[date_start_index:len(last_line)].strip() + + login_time = last_line[0:20] + separator = last_line[20:23] + logout_time = last_line[27:47] + + login = datetime.strptime(login_time, '%b %d %H:%M:%S %Y') + if separator == ' - ' and not logout_time.startswith(' '): + logout = datetime.strptime(logout_time, '%b %d %H:%M:%S %Y') + else: + logout = datetime.max + + records.append({ + 'user': user, + 'tty': terminal, + 'login': login, + 'logout': logout, + 'raw': last_line_raw + }) + + +while True: + event_raw = event_list.readline().strip() + if not event_raw: + break + + event = event_raw + event_time = datetime.strptime(event, '%Y-%m-%d %H:%M:%S') + + for record in records: + if event_time >= record['login'] and event_time <= record['logout']: + print(event_time, '{0:16} => {1}'.format(record['user'], record['raw'])) diff --git a/admin/ssh-bad-login.sh b/admin/ssh-bad-login.sh new file mode 100755 index 0000000..69519d6 --- /dev/null +++ b/admin/ssh-bad-login.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +if [ "$1" ]; then + since="$1" +else + since="`date "+%Y-%m-%d"`" +fi + +if [ "$2" ]; then + until="$2" +else + until="now" +fi + +journalctl _SYSTEMD_UNIT=sshd.service \ + --since="$since" --until="$until" | \ + grep 'Failed password for' | \ + sed -e 's/.*from\ //g' -e 's/\ port.*//g' | sort -n | { + count=0; + firstrun=0; + printed=0; + while read oneline; + do + printed=0; + if [ "$prevline" != "$oneline" ] && [ "$firstrun" = "1" ]; + then + echo "$count $prevline"; + count=0; + printed=1; + fi + count=$(($count+1)); + prevline="$oneline"; + firstrun=1; + done + if [ "$printed" = "0" ]; + then + echo "$count $prevline"; + fi } | \ + sort -nr diff --git a/admin/yumdb-reason-editor.py b/admin/yumdb-reason-editor.py new file mode 100755 index 0000000..9a47691 --- /dev/null +++ b/admin/yumdb-reason-editor.py @@ -0,0 +1,198 @@ +#!/usr/bin/env python2 + +import yum +from gi.repository import Gtk + + +class YumDB: + def __init__(self): + self.yb = yum.YumBase() + self.yb.conf + + def get_reasons(self, patterns=None): + reasons = [] + for pkg in sorted(self.yb.rpmdb.returnPackages(patterns=patterns)): + reasons.append({'nevra': pkg.nevra, \ + 'group_member': pkg.yumdb_info.group_member \ + if hasattr(pkg.yumdb_info, 'group_member') else '', \ + 'reason': pkg.yumdb_info.reason \ + if hasattr(pkg.yumdb_info, 'reason') else '<unset>', \ + 'reason_editor': pkg.yumdb_info.reason_editor \ + if hasattr(pkg.yumdb_info, 'reason_editor') else ''}) + return reasons + + def get_summary(self, nevra): + return self.yb.rpmdb.returnPackages(patterns=[ nevra ])[0].summary + + def get_description(self, nevra): + return self.yb.rpmdb.returnPackages(patterns=[ nevra ])[0].description + + def set_reason(self, nevra, value): + try: + pkg = self.yb.rpmdb.returnPackages(patterns=[ nevra ])[0] + except IndexError as e: + return '{} does not exists!'.format(nevra) + + try: + pkg.yumdb_info.reason = value + pkg.yumdb_info.reason_editor = 'modified' + except Exception as e: + return '{} fails: {}'.format(nevra, e[1]) + + return True + + +class PkgList(Gtk.TreeView): + def add_column(self, name, index): + renderer = Gtk.CellRendererText() + column = Gtk.TreeViewColumn(name) + column.pack_start(renderer, True) + column.add_attribute(renderer, 'text', index) + column.set_resizable(True) + column.set_sort_column_id(index) + column.set_sort_indicator(index) + self.append_column(column) + +class ReasonEditor(Gtk.Window): + COLUMN_NUMBER = 0 + COLUMN_NEVRA = 1 + COLUMN_GROUP = 2 + COLUMN_REASON = 3 + COLUMN_STATUS = 4 + + def __init__(self): + Gtk.Window.__init__(self, title='YumDB Reason Editor') + self.set_default_size(800, 600) + + self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5) + + self.pkg_store = Gtk.ListStore(int, str, str, str, str) + self.pkg_list = PkgList(self.pkg_store) + self.pkg_list.add_column('#', 0) + self.pkg_list.add_column('Name-Version-Arch', 1) + self.pkg_list.add_column('Group', 2) + self.pkg_list.add_column('Reason', 3) + self.pkg_list.add_column('Status', 4) + self.pkg_list.set_enable_search(False) + self.pkg_list.set_search_column(self.COLUMN_NEVRA) + self.pkg_list.connect('row-activated', self.toggle_reason) + + self.pkg_scrollable = Gtk.ScrolledWindow() + self.pkg_scrollable.add(self.pkg_list) + + self.count_label = Gtk.Label() + + self.bottom_buttons = Gtk.Box() + self.reload_db = Gtk.Button.new_with_mnemonic(label='_Reload YumDB') + self.reload_db.connect('clicked', self.load_database) + self.show_info = Gtk.Button.new_with_mnemonic(label='_Info') + self.show_info.connect('clicked', self.show_summary_description) + self.enable_search = Gtk.ToggleButton('_Search Disabled') + self.enable_search.connect('toggled', self.toggle_search) + self.enable_search.set_use_underline(True) + self.bottom_buttons.pack_start(self.reload_db, True, True, 0) + self.bottom_buttons.pack_start(self.show_info, True, True, 0) + self.bottom_buttons.pack_start(self.enable_search, True, True, 0) + + self.vbox.pack_start(self.pkg_scrollable, True, True, 0) + self.vbox.pack_start(self.count_label, False, False, 0) + self.vbox.pack_start(self.bottom_buttons, False, False, 0) + self.add(self.vbox) + self.show_all() + + self.load_database() + + def load_database(self, unused=None): + self.yumdb = YumDB() + self.pkg_store.clear() + + print('Loading database ...') + data = self.yumdb.get_reasons() + + print('Adding entries ...') + self.dep_count = 0 + self.user_count = 0 + for index, entry in enumerate(data): + self.pkg_store.append([ index + 1, + entry['nevra'], entry['group_member'], + entry['reason'], entry['reason_editor'] ]) + if entry['reason'] == 'dep': + self.dep_count += 1 + else: + self.user_count += 1 + + self.pkg_count = index + 1 + self.update_count() + self.show_all() + + def update_count(self): + self.count_label.set_label('{} dep, {} user, {} pkgs'.format( + self.dep_count, self.user_count, self.pkg_count)) + + def toggle_search(self, unused=None): + if self.enable_search.get_active(): + self.enable_search.set_label('_Search Enabled') + self.pkg_list.set_enable_search(True) + else: + self.enable_search.set_label('_Search Disabled') + self.pkg_list.set_enable_search(False) + + def toggle_reason(self, path, column, unused=None): + model = self.pkg_list.get_model() + tree_path = self.pkg_list.get_cursor()[0] + if tree_path == None: + return + + tree_iter = model.get_iter(tree_path) + if tree_iter == None: + return + + nevra = model[tree_iter][self.COLUMN_NEVRA] + is_dep = model[tree_iter][self.COLUMN_REASON] == 'dep' + + result = self.yumdb.set_reason(nevra, 'user' if is_dep else 'dep') + if result != True: + error_dialog = Gtk.MessageDialog( + title='YumDB Error', + buttons=Gtk.ButtonsType.CLOSE, + message_type=Gtk.MessageType.ERROR, + message_format=result) + error_dialog.run() + error_dialog.destroy() + return + + new_entry = self.yumdb.get_reasons([ nevra ])[0] + model[tree_iter][self.COLUMN_REASON] = new_entry['reason'] + model[tree_iter][self.COLUMN_STATUS] = new_entry['reason_editor'] + tree_path.next() + self.pkg_list.set_cursor(tree_path) + if is_dep: + self.dep_count -= 1 + self.user_count += 1 + else: + self.dep_count += 1 + self.user_count -= 1 + self.update_count() + + def show_summary_description(self, unused=None): + model, tree_iter = self.pkg_list.get_selection().get_selected() + if tree_iter == None: + return + nevra = model[tree_iter][1] + + dialog = Gtk.MessageDialog( + title=nevra, + buttons=Gtk.ButtonsType.CLOSE, + message_type=Gtk.MessageType.INFO, + message_format=None) + dialog.set_markup('<big><b>{}</b></big>\n\n{}'.format( + self.yumdb.get_summary(nevra), + self.yumdb.get_description(nevra))) + dialog.run() + dialog.destroy() + + +if __name__ == '__main__': + editor = ReasonEditor() + editor.connect('delete-event', Gtk.main_quit) + Gtk.main() |