expose active usernames

trunk
vilmibm 2020-06-18 20:24:25 +00:00
parent 13f9932084
commit 6644d7a806
1 changed files with 13 additions and 12 deletions

25
main.go
View File

@ -22,7 +22,6 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -59,6 +58,7 @@ type tildeData struct {
// Town Additions // Town Additions
LiveUserCount int `json:"live_user_count"` // Users who have changed their index.html LiveUserCount int `json:"live_user_count"` // Users who have changed their index.html
ActiveUserCount int `json:"active_user_count"` // Users with an active session ActiveUserCount int `json:"active_user_count"` // Users with an active session
ActiveUsers []string `json:"active_users"` // Usernames of logged in users
GeneratedAt string `json:"generated_at"` // When this was generated in '%Y-%m-%d %H:%M:%S' format GeneratedAt string `json:"generated_at"` // When this was generated in '%Y-%m-%d %H:%M:%S' format
GeneratedAtSec int64 `json:"generated_at_sec"` // When this was generated in seconds since epoch GeneratedAtSec int64 `json:"generated_at_sec"` // When this was generated in seconds since epoch
Uptime string `json:"uptime"` // output of `uptime -p` Uptime string `json:"uptime"` // output of `uptime -p`
@ -291,17 +291,17 @@ func liveUserCount(users []*user) int {
return count return count
} }
func activeUserCount() (int, error) { func activeUsers() ([]string, error) {
out, err := exec.Command("sh", "-c", "who | cut -d' ' -f1 | wc -l").Output() out, err := exec.Command("sh", "-c", "who | cut -d' ' -f1 | sort -u").Output()
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to get active user count: %w", err) return nil, fmt.Errorf("failed to get active user count: %w", err)
} }
count, err := strconv.Atoi(strings.TrimSpace(string(out))) scanner := bufio.NewScanner(bytes.NewReader(out))
if err != nil { usernames := []string{}
return 0, fmt.Errorf("could not parse active user count: %w", err) for scanner.Scan() {
usernames = append(usernames, strings.TrimSpace(scanner.Text()))
} }
return usernames, nil
return count, nil
} }
func getUptime() (string, error) { func getUptime() (string, error) {
@ -317,9 +317,9 @@ func tdp() (tildeData, error) {
if err != nil { if err != nil {
return tildeData{}, fmt.Errorf("could not get user list: %s", err) return tildeData{}, fmt.Errorf("could not get user list: %s", err)
} }
activeUsers, err := activeUserCount() activeUsernames, err := activeUsers()
if err != nil { if err != nil {
return tildeData{}, fmt.Errorf("could not count non-default users: %s", err) return tildeData{}, fmt.Errorf("could not count active users: %s", err)
} }
news, err := getNews() news, err := getNews()
if err != nil { if err != nil {
@ -343,7 +343,8 @@ func tdp() (tildeData, error) {
UserCount: len(users), UserCount: len(users),
Users: users, Users: users,
LiveUserCount: liveUserCount(users), LiveUserCount: liveUserCount(users),
ActiveUserCount: activeUsers, ActiveUserCount: len(activeUsernames),
ActiveUsers: activeUsernames,
Uptime: uptime, Uptime: uptime,
News: news, News: news,
GeneratedAt: time.Now().UTC().Format("2006-01-02 15:04:05"), GeneratedAt: time.Now().UTC().Format("2006-01-02 15:04:05"),