Add scripts

This commit is contained in:
kuben
2019-11-03 14:28:22 +01:00
parent fc0ee648e0
commit a58cdb6355
9 changed files with 1620 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
#!/bin/bash
# To generate list of large packages
# pacman -Qi | grep 'Name\|Size\|Description' | cut -d: -f2 | paste - - - | awk -F'\t' 'BEGIN{ s["MiB"]=1024; s["KiB"]=1;} {split($3, a, " "); print a[1] * s[a[2]], "KiB", $1}' | sort -n
LARGE_PKGS='linux jdk10-openjdk jre8-openjdk-headless jre10-openjdk-headless gimp gcc-list webkit2gtk gcc python chromium dropbox spotify mono bazel valgrind texlive-core linux-firmware'
echo -n "==> Looking for old packages to prune"
paccache -rk1
echo -n "==> Looking for old large packages to prune"
paccache -rk0 $LARGE_PKGS

46
scripts/displays.sh Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/bash
#$1 is -
#echo "DISP:eDP1"
#exit 0
function primary() {
xrandr | grep primary | awk '{print $1}'
}
#displays=($(xrandr | sed -rn 's/(^|(.* ))([^ ]*) connected(( .*)|$)/\3/g; T; p' ))
displays=$(xrandr | grep " connected" | awk '{print $1}')
case "$1" in
"connected")
echo "$displays"
# for i in "${displays[@]}"; do
# echo "$i"
# done
;;
"lemonbar")
echo "DISP ${displays[@]}" | sed "s/ /:/g"
;;
*)
# -F Fix strings, -x exactly, -q quiet
if echo "$displays" | grep -Fqx "$1"; then
case "$2" in
"off")
action="--off"
;;
"mirror")
action="--same-as $(primary)"
;;
"left")
action="--left-of $(primary) --auto"
;;
"right")
action="--right-of $(primary) --auto"
;;
*)
exit 1
;;
esac
xrandr --output $1 $action
fi
esac

1140
scripts/health/du.c Normal file

File diff suppressed because it is too large Load Diff

220
scripts/health/health.c Normal file
View File

@@ -0,0 +1,220 @@
#define DEBUG
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include "health.h"
#include "utils.h"
//ustring ok_str = NEW_USTRING("OK");
//ustring warn_str = NEW_USTRING("WARN");
//ustring arrow_str = NEW_USTRING("├─");
//ustring arrow_last_str = NEW_USTRING("└─");
ustring ok_str = {.str = "OK", .bytes = strlen("OK") };
ustring warn_str = {.str = "WARN ☠", .bytes = strlen("WARN ☠")};
ustring arrow_str = {.str = "├─", .bytes = strlen("├─")};
ustring arrow_last_str = {.str ="└─", .bytes = strlen("└─")};
// fts_path doesn't append trailing slash
struct dir_status top_dir_var[] = {
WATCH_DIR("/var/cache", "1.5G"),
WATCH_DIR("/var/log", "0.5G")
};
struct dir_status top_dir_home[] = {
WATCH_DIR("/home/kuba/.cache", "3G"),
WATCH_DIR("/home/kuba/Downloads", "1G")
};
struct dir_status top_dir_usr[] = {
WATCH_DIR("/usr/local", "5G"),
WATCH_DIR("/usr/lib", "5G"),
WATCH_DIR("/usr/share", "5G"),
WATCH_DIR("/usr/bin", "1G")
};
struct dir_status watched_dirs[] = {
WATCH_DIR_SUBS("/var", "2.5G", top_dir_var),
WATCH_DIR_SUBS("/home/kuba", "20G", top_dir_home),
WATCH_DIR_SUBS("/usr", "15G", top_dir_usr),
WATCH_DIR("/boot", "80M"),
WATCH_DIR("/etc", "20M"),
WATCH_DIR("/root", "10M"),
WATCH_DIR("/run", "10M"),
WATCH_DIR("/srv", "1M"),
WATCH_DIR("/opt", "1G")
};
int main(int argc, char* const argv[])
{
INIT_USTRING(ok_str);
INIT_USTRING(warn_str);
INIT_USTRING(arrow_str);
INIT_USTRING(arrow_last_str);
// Traverse top directories and do some nice formatting
char buf[80];
for (int i = 0; i < sizeof(watched_dirs)/sizeof(struct dir_status); i++){
struct dir_status *dir = watched_dirs + i;
disk_usage(dir);
format_directory_entry(buf, dir, 0, 0);
}
exit(0);
if (argc<2)
{
printf("Usage: %s <path-spec>\n", argv[0]);
exit(255);
}
return 0;
}
//Maybe just remove this, the effect is not noticable
off_t traverse_nochildren(FTS *file_system, const short dir_level)
{
FTSENT *node = NULL;
struct stat *st = NULL;
off_t tot_size = 0;
while( (node = fts_read(file_system)) != NULL)
{
// Add to size
st = node->fts_statp;
if (st) tot_size += (off_t) 512*st->st_blocks;
else debug("st null pointer, skipping\n");
if (node->fts_info == FTS_DP)
{
indent_debug(node->fts_level);
debug("Exiting %s\n", node->fts_path);
// Directory being visited in post-order. Only action is to
// return if back at the starting level
if (node->fts_level == dir_level) return tot_size;
}
}
return 0;// This is bad
}
void traverse_children(FTS *file_system, struct dir_status *dir, const short dir_level)
{
FTSENT *node = NULL;
struct stat *st = NULL;
off_t tot_size = 0;
top_loop:while( (node = fts_read(file_system)) != NULL)
{
//indent_debug(node->fts_level);
//debug("%s\n", node->fts_name);
if (node->fts_info == FTS_D)
{
if(strcmp(node->fts_path, dir->path) == 0) {
continue; // Do nothing
}
indent_debug(node->fts_level);
debug("Entering %s\n", node->fts_path);
// Directory being visited in pre-order. Do not count size except
// after returning from a recursion step. Determine if the current
// node is one of the children.
for (int i = 0; i < dir->n_children; i++) {
struct dir_status *sub_dir = dir->children+i;
if(strcmp(node->fts_path, sub_dir->path) == 0){
// Entering one of the child-directories
indent_debug(node->fts_level);
debug("Entering special %s\n", node->fts_path);
traverse_children(file_system, sub_dir, node->fts_level);
tot_size += sub_dir->size;
goto top_loop;
}
}
// Child directory not in list, call traverse_nochildren instead
tot_size += traverse_nochildren(file_system, node->fts_level);
continue; // Does the same as goto above
}
// Add to size
st = node->fts_statp;
if (st) tot_size += (off_t) 512*st->st_blocks;
else debug("st null pointer, skipping\n");
if (node->fts_info == FTS_DP)
{
indent_debug(node->fts_level);
debug("Exiting %s\n", node->fts_path);
// Directory being visited in post-order. Only action is to save
// size and return if back at the starting level
if (node->fts_level == dir_level){
dir->size = tot_size;
return;
}
}
}
}
int disk_usage(struct dir_status *top_dir)
{
char *paths[] = { top_dir->path, NULL};
FTS* file_system = NULL;
file_system = fts_open(paths, FTS_PHYSICAL, NULL);
if (file_system == NULL) return 1;
traverse_children(file_system, top_dir, 0);
fts_close(file_system);
return 0;
}
const int max_length = 60;
const int max_path_length = 40;
void format_directory_entry(char *buf, struct dir_status *dir, int level, int last_subdir)
{
int n = 0; //Keeps track of where in buffer we are
int delta_n = 0; // Keeps track of how much shorter the string appears
// than the number of bytes
// First fill with apropriate amount of spaces and └ or ├ charcters
if (level > 0) {
ustring *arr = last_subdir?&arrow_last_str:&arrow_str;
n = 4*level - arr->chars;
delta_n += arr->chars - arr->bytes;
memset(buf, ' ', n);
strncpy(buf+n, arr->str, arr->bytes);
n += arr->bytes;
}
// Path and size
n += snprintf(buf + n, max_path_length, "%s -- ", dir->path);
n += pretty_bytes(buf + n, dir->size);
memset(buf + n, ' ', max_length - n); // max_length - n for simplicity
// Right-justify WARN or OK status
ustring *ok = directory_ok(dir->size, dir->warn_at)?&ok_str:&warn_str;
delta_n += ok->chars - ok->bytes;
snprintf(buf + max_length - ok->bytes - 2 - delta_n, ok->bytes+2
, "%s\n", ok->str);// 2 leaves space for \n and null
printf("%s", buf);
// Loop through children
for (int i = 0; i < dir->n_children; i++){
int last = (i == dir->n_children - 1);
format_directory_entry(buf, dir->children + i, (level + 1), last);
}
if(level == 0) printf("\n");
}
int directory_ok(off_t size, char *warn_at){
off_t warn = pretty_bytes_to_num(warn_at);
return size < warn;
}

43
scripts/health/health.h Normal file
View File

@@ -0,0 +1,43 @@
#ifndef HEALTH_H
#define HEALTH_H
#include <sys/types.h>
#include "utils.h"
/*
* path - Path of the directory without trailing slash
* warn_at - How large is the directory allowed to be before a warning is
* issued. In human readable form (see utils.c:pretty_bytes)
* children - List of all watched sub-directories
* n_children - Number of elements in children
* size - Size of directory, this is filled in by disk_usage()
*/
struct dir_status {
char * const path;
char * const warn_at;
struct dir_status *children;
int n_children;
off_t size;
};
#define WATCH_DIR(pth,wrn) {.path = pth, .warn_at = wrn, .n_children=0, .size=0}
#define WATCH_DIR_SUBS(pth,wrn,subs) {.path = pth, .warn_at = wrn, .size=0 \
, .children=subs, .n_children=sizeof(subs)/sizeof(struct dir_status)}
off_t traverse_nochildren(FTS *file_system, const short dir_level);
void traverse_children(FTS *file_system, struct dir_status *dir, const short dir_level);
int disk_usage(struct dir_status *top_dir);
//ustring ok_ustr = {.str = "OK", .bytes = 2, .chars = 2};
//NEW_USTRING(ok_str, "OK");
//NEW_USTRING(warn_str, "WARN");
//NEW_USTRING(arrow_str, "├─");
//NEW_USTRING(arrow_last_str,"└─");
//INIT_USTRING(ok_str);
//INIT_USTRING(warn_str);
//INIT_USTRING(arrow_str);
//INIT_USTRING(arrow_last_str);
void format_directory_entry(char *buf, struct dir_status *dir, int level, int last_subdir);
int directory_ok(off_t size, char *warn_at);
#endif

24
scripts/health/makefile Executable file
View File

@@ -0,0 +1,24 @@
CFLAGS = -Wall
LDFLAGS = -lm
CC = gcc
default: health
optim: CFLAGS += -O3
optim: health
debug: CFLAGS += -g
debug: health
OBJECTS=health.o utils.o
health: $(OBJECTS)
$(CC) $(CFLAGS) -o health $(OBJECTS) $(LDFLAGS)
health.o: health.c health.h
$(CC) $(CFLAGS) -c health.c
utils.o: utils.c utils.h
$(CC) $(CFLAGS) -c utils.c
clean:
rm -f *.o

87
scripts/health/utils.c Normal file
View File

@@ -0,0 +1,87 @@
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <math.h>
#include "utils.h"
void indent(int i)
{
for (; i > 0; i--) printf(" ");
}
// Prints to the provided buffer a nice number of bytes (KB, MB, GB, etc)
// Returns bytes written
int pretty_bytes(char* buf, long long bytes)
{
const char* suffixes[7];
suffixes[0] = "B";
suffixes[1] = "KB";
suffixes[2] = "MB";
suffixes[3] = "GB";
suffixes[4] = "TB";
suffixes[5] = "PB";
suffixes[6] = "EB";
uint s = 0; // which suffix to use
double count = bytes;
while (count >= 1024 && s < 7)
{
s++;
count /= 1024;
}
if (count - floor(count) == 0.0)
return sprintf(buf, "%d %s", (int)count, suffixes[s]);
else
return sprintf(buf, "%.1f %s", count, suffixes[s]);
}
long long pretty_bytes_to_num(char* buf)
{
// First letter of suffix in order
const char suffixes[7] = {'B', 'K', 'M', 'G', 'T', 'P', 'E'};
char suffix;
float fnum;
long long num = 1;
int n;
errno = 0;
n = sscanf(buf,"%f%c", &fnum, &suffix);
if (n == 2) {
//TODO error handling
} else if (errno != 0) {
perror("scanf");
} else {
fprintf(stderr, "No matching characters\n");
}
for (int s=0;s<sizeof(suffixes); s++){
if (suffix == suffixes[s]) break;
num *= 1024;
}
return (long long )(num*fnum);
}
size_t utf8len(const char *s)
{
size_t len = 0;
for (; *s; ++s) if ((*s & 0xC0) != 0x80) ++len;
return len;
}
void debug(const char *format, ...){
#ifdef DEBUG
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
#endif
}
void indent_debug(int i){
#ifdef DEBUG
indent(i);
#endif
}

25
scripts/health/utils.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef UTILS_H
#define UTILS_H
#include <sys/types.h>
#include <string.h>
size_t utf8len(const char *s);
typedef struct {
const char *str;
const size_t bytes;
size_t chars;
} ustring;
#define NEW_USTRING(string) {.str = string, .bytes = strlen(string)}//; varname.chars=utf8len(string)}
#define INIT_USTRING(varname) (varname).chars = utf8len(varname.str)
long long pretty_bytes_to_num(char* buf);
int pretty_bytes(char* buf, long long bytes);
void debug(const char *format, ...);
void indent (int i);
void indent_debug (int i);
#endif

24
scripts/set_bg.sh Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
#function get_index(){
# for i in "${!xra_out[@]}"; do
# if [[ "${xra_out[$i]}" = "$value" ]]; then
# echo "${i}";
# break
# fi
# done
#}
#xra_out=($(xrandr | grep ' connected' | awk '{print $1}'))
#in=($@)
#for i in `seq 0 2 ${#in[@]}`; do
# value="${in[$i]}"
# idx=$(get_index)
# if [ ! -z "$idx" ]; then
# screen_idx="$idx"
# #"${xra_out[$idx-1]}"
# paths[${screen_idx%:}]="--bg-scale ${in[$i+1]}"
# fi
#done
##echo "${paths[@]}"
#str=`printf -v var "%s\n" "${System[@]}"`
#sh -c "feh ${paths[@]}"
feh --bg-scale "$@"