Diff -- /var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/Stats.pm
Diff
/var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/Stats.pm
added on local at 2026-07-10 10:11:47
Added
+0
lines
Removed
-0
lines
Context
367
unchanged
Blobs
from 7e20eaaa93e5
to 7e20eaaa93e5
to 7e20eaaa93e5
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | package MODS::MetaAdmin::Stats; |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # Meta-Admin -- server stats reader. Plain shell + /proc reads, no |
| 4 | 4 | # CPAN deps. Returns plain hashrefs that server.cgi feeds into |
| 5 | 5 | # hand-rolled SVG graphs. |
| 6 | 6 | #====================================================================== |
| 7 | 7 | use strict; |
| 8 | 8 | use warnings; |
| 9 | 9 | |
| 10 | 10 | sub new { return bless({}, shift); } |
| 11 | 11 | |
| 12 | 12 | sub _shell { |
| 13 | 13 | my $cmd = shift; |
| 14 | 14 | my $out = `$cmd 2>/dev/null`; |
| 15 | 15 | $out = '' unless defined $out; |
| 16 | 16 | chomp $out; |
| 17 | 17 | return $out; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | # _shell_du: run `du -sb <path>` via sudo if a narrow sudoers entry |
| 21 | 21 | # is installed (see /etc/sudoers.d/meta_admin_stats). Falls back to |
| 22 | 22 | # unprivileged du if sudo -n fails -- caller still gets a number, |
| 23 | 23 | # just under-reported for paths owned by other Plesk users. |
| 24 | 24 | sub _shell_du { |
| 25 | 25 | my $path = shift; |
| 26 | 26 | return 0 unless defined $path && length $path; |
| 27 | 27 | return 0 if $path =~ /[`;\$\|]/; |
| 28 | 28 | my $out = `sudo -n /usr/bin/du -sb '$path' 2>/dev/null | awk '{print \$1}'`; |
| 29 | 29 | chomp $out; |
| 30 | 30 | if (!$out || $out !~ /^\d+$/) { |
| 31 | 31 | # Sudo unavailable -- best-effort as the CGI user. |
| 32 | 32 | $out = `du -sb '$path' 2>/dev/null | awk '{print \$1}'`; |
| 33 | 33 | chomp $out; |
| 34 | 34 | } |
| 35 | 35 | return ($out && $out =~ /^\d+$/) ? ($out + 0) : 0; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | #--------------------------------------------------------------------- |
| 39 | 39 | # Disk on the root partition. Returns { total_b, used_b, avail_b }. |
| 40 | 40 | #--------------------------------------------------------------------- |
| 41 | 41 | sub disk_root { |
| 42 | 42 | my $self = shift; |
| 43 | 43 | return $self->disk_for('/'); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | #--------------------------------------------------------------------- |
| 47 | 47 | # Disk stats for any mount path. Returns { total_b, used_b, avail_b }. |
| 48 | 48 | # Used for the "Sites partition" module -- portfolio sites live on |
| 49 | 49 | # /var so the meaningful headline number is /var, not /. |
| 50 | 50 | #--------------------------------------------------------------------- |
| 51 | 51 | sub disk_for { |
| 52 | 52 | my ($self, $path) = @_; |
| 53 | 53 | $path //= '/'; |
| 54 | 54 | return { total_b => 0, used_b => 0, avail_b => 0 } |
| 55 | 55 | if $path =~ /[`;\$\|]/; # shell-injection belt-and-suspenders |
| 56 | 56 | my $line = _shell("df -B1 '$path' | tail -1"); |
| 57 | 57 | my @p = split /\s+/, $line; |
| 58 | 58 | return { total_b => 0, used_b => 0, avail_b => 0 } unless @p >= 4; |
| 59 | 59 | return { |
| 60 | 60 | total_b => $p[1] + 0, |
| 61 | 61 | used_b => $p[2] + 0, |
| 62 | 62 | avail_b => $p[3] + 0, |
| 63 | 63 | }; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | #--------------------------------------------------------------------- |
| 67 | 67 | # Where is /var space going? du -sb on a handful of well-known |
| 68 | 68 | # top-level subdirs so the caller can render a stacked-bar or |
| 69 | 69 | # categorized-list "what's eating /var" breakdown. Returns arrayref |
| 70 | 70 | # of { label, path, used_b, color } biggest-first, with an implicit |
| 71 | 71 | # "Other" bucket = /var - sum(named). |
| 72 | 72 | #--------------------------------------------------------------------- |
| 73 | 73 | sub var_breakdown { |
| 74 | 74 | my $self = shift; |
| 75 | 75 | # Non-overlapping top-level probes -- each path is a direct child of |
| 76 | 76 | # /var so there's no double-counting into the "Other" bucket. |
| 77 | 77 | my @probes = ( |
| 78 | 78 | { label => 'Databases + Plesk state', path => '/var/lib', color => '#7c3aed' }, |
| 79 | 79 | { label => 'Web hosting', path => '/var/www', color => '#b53048' }, |
| 80 | 80 | { label => 'Mail spool', path => '/var/qmail', color => '#06b6d4' }, |
| 81 | 81 | { label => 'Plesk cache', path => '/var/cache', color => '#059669' }, |
| 82 | 82 | { label => 'Logs', path => '/var/log', color => '#92400e' }, |
| 83 | 83 | { label => 'Imunify360 security', path => '/var/imunify360', color => '#f97316' }, |
| 84 | 84 | { label => 'Spool + tmp', path => '/var/spool', color => '#a855f7' }, |
| 85 | 85 | ); |
| 86 | 86 | my @out; |
| 87 | 87 | my $named_sum = 0; |
| 88 | 88 | foreach my $p (@probes) { |
| 89 | 89 | my $b = _shell_du($p->{path}); |
| 90 | 90 | next unless $b > 0; |
| 91 | 91 | push @out, { %$p, used_b => $b }; |
| 92 | 92 | $named_sum += $b; |
| 93 | 93 | } |
| 94 | 94 | @out = sort { $b->{used_b} <=> $a->{used_b} } @out; |
| 95 | 95 | |
| 96 | 96 | # "Other" bucket = /var total-used minus everything we named. |
| 97 | 97 | my $var = $self->disk_for('/var'); |
| 98 | 98 | my $other = ($var->{used_b} || 0) - $named_sum; |
| 99 | 99 | push @out, { |
| 100 | 100 | label => 'Other', |
| 101 | 101 | path => '/var (uncategorized)', |
| 102 | 102 | used_b => ($other > 0 ? $other : 0), |
| 103 | 103 | color => '#4a1a20', |
| 104 | 104 | } if $other > 0; |
| 105 | 105 | |
| 106 | 106 | return \@out; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | #--------------------------------------------------------------------- |
| 110 | 110 | # All mounted filesystems (skip RAM-backed pseudo-FS like tmpfs/devtmpfs). |
| 111 | 111 | # Returns arrayref of { mount, fstype, device, total_b, used_b, avail_b, |
| 112 | 112 | # used_pct }, ordered biggest-first. Empty on failure. |
| 113 | 113 | #--------------------------------------------------------------------- |
| 114 | 114 | sub partitions { |
| 115 | 115 | my $self = shift; |
| 116 | 116 | # -P = POSIX (one row per FS, no wrapping), -T = include Type column, |
| 117 | 117 | # -B1 = bytes. Columns: source fstype 1-blocks used avail capacity mount. |
| 118 | 118 | my $out = _shell('df -PTB1 2>/dev/null'); |
| 119 | 119 | my @rows; |
| 120 | 120 | foreach my $line (split /\n/, $out) { |
| 121 | 121 | next if $line =~ /^Filesystem\s+Type/; # header |
| 122 | 122 | my @p = split /\s+/, $line; |
| 123 | 123 | next unless @p >= 7; |
| 124 | 124 | my ($src, $fs, $total, $used, $avail, undef, $mnt) = @p; |
| 125 | 125 | # Real filesystems only -- skip RAM / cgroup / overlay pseudo-mounts. |
| 126 | 126 | next if $fs =~ /^(tmpfs|devtmpfs|cgroup|cgroup2|proc|sysfs|debugfs|tracefs|securityfs|pstore|autofs|overlay|squashfs|efivarfs|fuse\.\w+|nsfs|mqueue|hugetlbfs|configfs|bpf|ramfs|none)$/; |
| 127 | 127 | next if $src eq 'none'; |
| 128 | 128 | $total += 0; $used += 0; $avail += 0; |
| 129 | 129 | next unless $total > 0; |
| 130 | 130 | my $pct = int(($used / $total) * 100 + 0.5); |
| 131 | 131 | push @rows, { |
| 132 | 132 | device => $src, |
| 133 | 133 | fstype => $fs, |
| 134 | 134 | mount => $mnt, |
| 135 | 135 | total_b => $total, |
| 136 | 136 | used_b => $used, |
| 137 | 137 | avail_b => $avail, |
| 138 | 138 | used_pct => $pct, |
| 139 | 139 | }; |
| 140 | 140 | } |
| 141 | 141 | @rows = sort { $b->{total_b} <=> $a->{total_b} } @rows; |
| 142 | 142 | return \@rows; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | #--------------------------------------------------------------------- |
| 146 | 146 | # Physical disk layout. lsblk in JSON when available (util-linux 2.27+), |
| 147 | 147 | # else fall back to plain text. Returns arrayref of top-level disks: |
| 148 | 148 | # { name, size_b, model, is_raid_member }. Empty on failure. |
| 149 | 149 | #--------------------------------------------------------------------- |
| 150 | 150 | sub physical_disks { |
| 151 | 151 | my $self = shift; |
| 152 | 152 | my $out = _shell('lsblk -b -d -n -o NAME,SIZE,TYPE,MODEL 2>/dev/null'); |
| 153 | 153 | my @disks; |
| 154 | 154 | foreach my $line (split /\n/, $out) { |
| 155 | 155 | # Split with limit=4 so MODEL can contain spaces |
| 156 | 156 | my ($name, $size, $type, $model) = split /\s+/, $line, 4; |
| 157 | 157 | next unless $name && $type && $type eq 'disk'; |
| 158 | 158 | $model //= ''; $model =~ s/\s+$//; |
| 159 | 159 | push @disks, { |
| 160 | 160 | name => $name, |
| 161 | 161 | size_b => ($size || 0) + 0, |
| 162 | 162 | model => $model || '', |
| 163 | 163 | }; |
| 164 | 164 | } |
| 165 | 165 | return \@disks; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | #--------------------------------------------------------------------- |
| 169 | 169 | # site_footprints -- combined per-site disk + RAM view for the Websites |
| 170 | 170 | # Folders / Sites RAM modules on /server.cgi. Returns arrayref of |
| 171 | 171 | # { slug, display_name, brand_color, vhost_path, owner_user, |
| 172 | 172 | # disk_b, ram_b } sorted by disk usage desc. |
| 173 | 173 | # |
| 174 | 174 | # RAM per site is the sum of RSS (bytes) of every currently-live |
| 175 | 175 | # process owned by that vhost's Linux user (from stat -c %U). CGI |
| 176 | 176 | # sites hold no RAM between requests -- if you see 0 for one, the |
| 177 | 177 | # CGI just isn't executing right now. |
| 178 | 178 | #--------------------------------------------------------------------- |
| 179 | 179 | sub site_footprints { |
| 180 | 180 | my ($self, $sites) = @_; |
| 181 | 181 | $sites ||= []; |
| 182 | 182 | |
| 183 | 183 | # One ps sweep, aggregate RSS (kB) by owner user. |
| 184 | 184 | my %ram_by_user; |
| 185 | 185 | foreach my $line (split /\n/, _shell('ps -eo user:32,rss --no-headers 2>/dev/null')) { |
| 186 | 186 | $line =~ s/^\s+//; |
| 187 | 187 | my ($user, $rss_kb) = split /\s+/, $line, 2; |
| 188 | 188 | next unless $user && defined $rss_kb && $rss_kb =~ /^\d+$/; |
| 189 | 189 | $ram_by_user{$user} += $rss_kb + 0; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | my @out; |
| 193 | 193 | foreach my $srow (@$sites) { |
| 194 | 194 | my $path = $srow->{vhost_path} or next; |
| 195 | 195 | next unless $path =~ m{^/}; |
| 196 | 196 | next if $path =~ /[`;\$\|]/; # belt-and-suspenders shell-injection guard |
| 197 | 197 | |
| 198 | 198 | # stat first -- if it fails the vhost user disallows traversal |
| 199 | 199 | # from this Perl process, so du will also fail. Mark disk/ram |
| 200 | 200 | # as undef (renderer shows "n/a" + a footnote). |
| 201 | 201 | my $stat_out = _shell("stat -c '%U' '$path' 2>&1"); |
| 202 | 202 | my $owner = ($stat_out =~ /^([A-Za-z_][A-Za-z0-9_-]{0,31})$/) ? $1 : ''; |
| 203 | 203 | my $unreachable = !$owner; |
| 204 | 204 | |
| 205 | 205 | my ($du_b, $ram_b); |
| 206 | 206 | if ($unreachable) { |
| 207 | 207 | $du_b = undef; |
| 208 | 208 | $ram_b = undef; |
| 209 | 209 | } else { |
| 210 | 210 | my $du_out = _shell("du -sb '$path' 2>/dev/null | awk '{print \$1}'"); |
| 211 | 211 | $du_b = ($du_out =~ /^\d+$/) ? ($du_out + 0) : 0; |
| 212 | 212 | $ram_b = exists $ram_by_user{$owner} |
| 213 | 213 | ? ($ram_by_user{$owner} * 1024) : 0; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | push @out, { |
| 217 | 217 | slug => $srow->{slug}, |
| 218 | 218 | display_name => $srow->{display_name}, |
| 219 | 219 | brand_color => $srow->{brand_color} || '#807aa8', |
| 220 | 220 | vhost_path => $path, |
| 221 | 221 | owner_user => $owner, |
| 222 | 222 | disk_b => $du_b, |
| 223 | 223 | ram_b => $ram_b, |
| 224 | 224 | unreachable => $unreachable, |
| 225 | 225 | }; |
| 226 | 226 | } |
| 227 | 227 | @out = sort { |
| 228 | 228 | ($b->{disk_b} // -1) <=> ($a->{disk_b} // -1) |
| 229 | 229 | } @out; |
| 230 | 230 | return \@out; |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | #--------------------------------------------------------------------- |
| 234 | 234 | # Per-vhost disk: du -sb on each path. Slow-ish on large vhosts but |
| 235 | 235 | # bounded (the dirs we're crawling are tens of MB to single-digit GB). |
| 236 | 236 | #--------------------------------------------------------------------- |
| 237 | 237 | sub disk_per_vhost { |
| 238 | 238 | my ($self, $paths) = @_; |
| 239 | 239 | $paths ||= []; |
| 240 | 240 | my @out; |
| 241 | 241 | foreach my $row (@$paths) { |
| 242 | 242 | my $path = $row->{vhost_path} or next; |
| 243 | 243 | next unless $path =~ m{^/}; |
| 244 | 244 | next if $path =~ /[`;\$\|]/; # belt-and-suspenders on shell injection |
| 245 | 245 | my $b = _shell("du -sb '$path' 2>/dev/null | awk '{print \$1}'"); |
| 246 | 246 | $b = ($b =~ /^\d+$/) ? ($b + 0) : 0; |
| 247 | 247 | push @out, { |
| 248 | 248 | slug => $row->{slug}, |
| 249 | 249 | display_name => $row->{display_name}, |
| 250 | 250 | brand_color => $row->{brand_color} || '#807aa8', |
| 251 | 251 | vhost_path => $path, |
| 252 | 252 | used_b => $b, |
| 253 | 253 | }; |
| 254 | 254 | } |
| 255 | 255 | @out = sort { $b->{used_b} <=> $a->{used_b} } @out; |
| 256 | 256 | return \@out; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | #--------------------------------------------------------------------- |
| 260 | 260 | # Memory. /proc/meminfo in KB. |
| 261 | 261 | #--------------------------------------------------------------------- |
| 262 | 262 | sub memory { |
| 263 | 263 | my $self = shift; |
| 264 | 264 | my %m; |
| 265 | 265 | if (open my $fh, '<', '/proc/meminfo') { |
| 266 | 266 | while (<$fh>) { |
| 267 | 267 | chomp; |
| 268 | 268 | if (/^(\S+):\s+(\d+)/) { |
| 269 | 269 | $m{$1} = $2 * 1024; # to bytes |
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | close $fh; |
| 273 | 273 | } |
| 274 | 274 | my $total = $m{MemTotal} || 0; |
| 275 | 275 | my $avail = $m{MemAvailable} || $m{MemFree} || 0; |
| 276 | 276 | my $used = $total - $avail; |
| 277 | 277 | my $cached = $m{Cached} || 0; |
| 278 | 278 | my $buf = $m{Buffers} || 0; |
| 279 | 279 | my $swap_t = $m{SwapTotal} || 0; |
| 280 | 280 | my $swap_f = $m{SwapFree} || 0; |
| 281 | 281 | my $swap_u = $swap_t - $swap_f; |
| 282 | 282 | return { |
| 283 | 283 | total_b => $total, used_b => $used, avail_b => $avail, |
| 284 | 284 | cached_b => $cached, buffers_b => $buf, |
| 285 | 285 | swap_total_b => $swap_t, swap_used_b => $swap_u, |
| 286 | 286 | }; |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | #--------------------------------------------------------------------- |
| 290 | 290 | # Load + uptime + cpu count. |
| 291 | 291 | #--------------------------------------------------------------------- |
| 292 | 292 | sub cpu { |
| 293 | 293 | my $self = shift; |
| 294 | 294 | my %r = ( cores => 0, load1 => 0, load5 => 0, load15 => 0, uptime_s => 0 ); |
| 295 | 295 | if (open my $fh, '<', '/proc/loadavg') { |
| 296 | 296 | my $line = <$fh>; close $fh; |
| 297 | 297 | chomp $line; |
| 298 | 298 | my @p = split /\s+/, $line; |
| 299 | 299 | $r{load1} = ($p[0] || 0) + 0; |
| 300 | 300 | $r{load5} = ($p[1] || 0) + 0; |
| 301 | 301 | $r{load15} = ($p[2] || 0) + 0; |
| 302 | 302 | } |
| 303 | 303 | if (open my $fh, '<', '/proc/cpuinfo') { |
| 304 | 304 | while (<$fh>) { $r{cores}++ if /^processor\s*:/; } |
| 305 | 305 | close $fh; |
| 306 | 306 | } |
| 307 | 307 | if (open my $fh, '<', '/proc/uptime') { |
| 308 | 308 | my $line = <$fh>; close $fh; |
| 309 | 309 | my ($up) = split /\s+/, $line; |
| 310 | 310 | $r{uptime_s} = ($up || 0) + 0; |
| 311 | 311 | } |
| 312 | 312 | return \%r; |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | #--------------------------------------------------------------------- |
| 316 | 316 | # Hostname + kernel + distro fingerprint. |
| 317 | 317 | #--------------------------------------------------------------------- |
| 318 | 318 | sub host { |
| 319 | 319 | my $self = shift; |
| 320 | 320 | return { |
| 321 | 321 | host => _shell('hostname'), |
| 322 | 322 | kernel => _shell('uname -r'), |
| 323 | 323 | os => _shell('grep PRETTY_NAME /etc/os-release | head -1 | sed -e \'s/^[^=]*=//\' -e \'s/"//g\''), |
| 324 | 324 | }; |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | #--------------------------------------------------------------------- |
| 328 | 328 | # Top 5 processes by CPU usage. Returns arrayref of { pid, user, cpu, |
| 329 | 329 | # mem, cmd }. |
| 330 | 330 | #--------------------------------------------------------------------- |
| 331 | 331 | sub top_processes { |
| 332 | 332 | my $self = shift; |
| 333 | 333 | my $out = _shell("ps -eo pid,user,pcpu,pmem,comm --sort=-pcpu | head -6 | tail -5"); |
| 334 | 334 | my @rows; |
| 335 | 335 | foreach my $line (split /\n/, $out) { |
| 336 | 336 | $line =~ s/^\s+//; |
| 337 | 337 | my ($pid, $user, $cpu, $mem, $cmd) = split /\s+/, $line, 5; |
| 338 | 338 | next unless defined $pid && $pid =~ /^\d+$/; |
| 339 | 339 | push @rows, { |
| 340 | 340 | pid => $pid, user => $user, cpu => ($cpu+0), mem => ($mem+0), cmd => $cmd, |
| 341 | 341 | }; |
| 342 | 342 | } |
| 343 | 343 | return \@rows; |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | sub human_size { |
| 347 | 347 | my $b = shift; $b = 0 unless defined $b && $b =~ /^-?\d+(\.\d+)?$/; |
| 348 | 348 | my $sign = $b < 0 ? '-' : ''; |
| 349 | 349 | my $a = abs($b); |
| 350 | 350 | if ($a >= 1024**4) { return sprintf('%s%.1f TB', $sign, $a / 1024**4); } |
| 351 | 351 | if ($a >= 1024**3) { return sprintf('%s%.1f GB', $sign, $a / 1024**3); } |
| 352 | 352 | if ($a >= 1024**2) { return sprintf('%s%.1f MB', $sign, $a / 1024**2); } |
| 353 | 353 | if ($a >= 1024) { return sprintf('%s%.1f KB', $sign, $a / 1024); } |
| 354 | 354 | return sprintf('%s%d B', $sign, $a); |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | sub human_uptime { |
| 358 | 358 | my $s = shift; $s = 0 unless $s; |
| 359 | 359 | my $d = int($s / 86400); |
| 360 | 360 | my $h = int(($s % 86400) / 3600); |
| 361 | 361 | my $m = int(($s % 3600) / 60); |
| 362 | 362 | return sprintf('%dd %dh %dm', $d, $h, $m) if $d; |
| 363 | 363 | return sprintf('%dh %dm', $h, $m) if $h; |
| 364 | 364 | return sprintf('%dm', $m); |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | 1; |