aboutsummaryrefslogtreecommitdiff
path: root/lib/subcommands/top.bash
blob: 7763eba6f99024187a80048d702c605cb0af2963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2023 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

subcommand()
{
	local results
	local widths

	declare -A results
	declare -A widths=(
		[load]=0
		[memory]=0
		[node]=0
		[storage]=0
		[uptime]=0
	)

	if [[ ! -d "$BASHTARD_ETCDIR/hosts.d" ]]
	then
		crit "$BASHTARD_NAME/ssh" "Could not find hosts file at $BASHTARD_ETCDIR/hosts.d"
		return 3
	fi

	chgdir "$BASHTARD_ETCDIR/hosts.d"

	for node in *
	do
		local user
		local host

		user="$(config_for "$node" "bashtard.ssh.user" "$USER")"
		host="$(config_for "$node" "bashtard.ssh.host")"

		if [[ -z "$host" ]]
		then
			crit "$BASHTARD_NAME/ssh" "bashtard.ssh.host is not configured for $node"
			continue
		fi

		if [[ "$node" == "${BASHTARD_PLATFORM[fqdn]}" ]]
		then
			results+=(["$node"]="$("$BASHTARD_BIN" sysinfo)")
		else
			debug "$BASHTARD_NAME/ssh" "$user@$node ($host) > bashtard sysinfo"
			# shellcheck disable=SC2029
			results+=(["$node"]="$(ssh "$user@$host" "bashtard sysinfo")")
		fi

		unset user
		unset host
	done

	# Check widths
	for node in "${!results[@]}"
	do
		node_load="$(grep '^load' <<< "${results["$node"]}" | sed 's/[^ ]* *//')"
		node_memory="$(grep '^memory' <<< "${results["$node"]}" | sed 's/[^ ]* *//')"
		node_storage="$(grep '^storage' <<< "${results["$node"]}" | sed 's/[^ ]* *//')"
		node_uptime="$(grep '^uptime' <<< "${results["$node"]}" | sed 's/[^ ]* *//')"

		width_load="$(wc -c <<< "$node_load")"
		width_memory="$(wc -c <<< "$node_memory")"
		width_node="$(wc -c <<< "$node")"
		width_storage="$(wc -c <<< "$node_storage")"
		width_uptime="$(wc -c <<< "$node_uptime")"

		if (( widths[load] < width_load ))
		then
			widths[load]=$width_load
		fi

		if (( widths[memory] < width_memory ))
		then
			widths[memory]=$width_memory
		fi

		if (( widths[node] < width_node ))
		then
			widths[node]=$width_node
		fi

		if (( widths[storage] < width_storage ))
		then
			widths[storage]=$width_storage
		fi

		if (( widths[uptime] < width_uptime ))
		then
			widths[uptime]=$width_uptime
		fi
	done

	# Print results
	printf "%-${widths[node]}s %-${widths[load]}s  %-${widths[memory]}s %-${widths[storage]}s %-${widths[uptime]}s\n" \
		"Node" \
		"Load" \
		"Memory" \
		"Storage" \
		"Uptime"

	for node in "${!results[@]}"
	do
		printf "%-${widths[node]}s %-${widths[load]}s %${widths[memory]}s %${widths[storage]}s %${widths[uptime]}s\n" \
			"$node" \
			"$(grep '^load' <<< "${results["$node"]}" | sed 's/[^ ]* *//')" \
			"$(grep '^memory' <<< "${results["$node"]}" | sed 's/[^ ]* *//')" \
			"$(grep '^storage' <<< "${results["$node"]}" | sed 's/[^ ]* *//')" \
			"$(grep '^uptime' <<< "${results["$node"]}" | sed 's/[^ ]* *//')"
	done | sort
}