expose active usernames
parent
13f9932084
commit
6644d7a806
25
main.go
25
main.go
|
@ -22,7 +22,6 @@ import (
|
|||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -59,6 +58,7 @@ type tildeData struct {
|
|||
// Town Additions
|
||||
LiveUserCount int `json:"live_user_count"` // Users who have changed their index.html
|
||||
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
|
||||
GeneratedAtSec int64 `json:"generated_at_sec"` // When this was generated in seconds since epoch
|
||||
Uptime string `json:"uptime"` // output of `uptime -p`
|
||||
|
@ -291,17 +291,17 @@ func liveUserCount(users []*user) int {
|
|||
return count
|
||||
}
|
||||
|
||||
func activeUserCount() (int, error) {
|
||||
out, err := exec.Command("sh", "-c", "who | cut -d' ' -f1 | wc -l").Output()
|
||||
func activeUsers() ([]string, error) {
|
||||
out, err := exec.Command("sh", "-c", "who | cut -d' ' -f1 | sort -u").Output()
|
||||
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)))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not parse active user count: %w", err)
|
||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||
usernames := []string{}
|
||||
for scanner.Scan() {
|
||||
usernames = append(usernames, strings.TrimSpace(scanner.Text()))
|
||||
}
|
||||
|
||||
return count, nil
|
||||
return usernames, nil
|
||||
}
|
||||
|
||||
func getUptime() (string, error) {
|
||||
|
@ -317,9 +317,9 @@ func tdp() (tildeData, error) {
|
|||
if err != nil {
|
||||
return tildeData{}, fmt.Errorf("could not get user list: %s", err)
|
||||
}
|
||||
activeUsers, err := activeUserCount()
|
||||
activeUsernames, err := activeUsers()
|
||||
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()
|
||||
if err != nil {
|
||||
|
@ -343,7 +343,8 @@ func tdp() (tildeData, error) {
|
|||
UserCount: len(users),
|
||||
Users: users,
|
||||
LiveUserCount: liveUserCount(users),
|
||||
ActiveUserCount: activeUsers,
|
||||
ActiveUserCount: len(activeUsernames),
|
||||
ActiveUsers: activeUsernames,
|
||||
Uptime: uptime,
|
||||
News: news,
|
||||
GeneratedAt: time.Now().UTC().Format("2006-01-02 15:04:05"),
|
||||
|
|
Loading…
Reference in New Issue