2017-05-11 13:04:43 +00:00
|
|
|
// +build !no_ws
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
import (
|
2017-06-10 07:58:15 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-09-03 04:50:31 +00:00
|
|
|
"fmt"
|
2017-06-10 07:58:15 +00:00
|
|
|
"net/http"
|
2017-09-03 04:50:31 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-07-17 10:23:42 +00:00
|
|
|
"github.com/Azareal/gopsutil/cpu"
|
|
|
|
"github.com/Azareal/gopsutil/mem"
|
2017-09-03 04:50:31 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2017-06-10 07:58:15 +00:00
|
|
|
)
|
2017-05-11 13:04:43 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type WS_User struct {
|
2017-05-11 13:04:43 +00:00
|
|
|
conn *websocket.Conn
|
|
|
|
User *User
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type WS_Hub struct {
|
|
|
|
onlineUsers map[int]*WS_User
|
|
|
|
onlineGuests map[*WS_User]bool
|
|
|
|
guests sync.RWMutex
|
|
|
|
users sync.RWMutex
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
var wsHub WS_Hub
|
|
|
|
var wsUpgrader = websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}
|
|
|
|
var errWsNouser = errors.New("This user isn't connected via WebSockets")
|
2017-05-11 13:04:43 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-09-03 04:50:31 +00:00
|
|
|
enableWebsockets = true
|
|
|
|
adminStatsWatchers = make(map[*WS_User]bool)
|
|
|
|
wsHub = WS_Hub{
|
|
|
|
onlineUsers: make(map[int]*WS_User),
|
|
|
|
onlineGuests: make(map[*WS_User]bool),
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func (hub *WS_Hub) guestCount() int {
|
2017-05-11 13:04:43 +00:00
|
|
|
defer hub.guests.RUnlock()
|
|
|
|
hub.guests.RLock()
|
2017-09-03 04:50:31 +00:00
|
|
|
return len(hub.onlineGuests)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func (hub *WS_Hub) userCount() int {
|
2017-05-11 13:04:43 +00:00
|
|
|
defer hub.users.RUnlock()
|
|
|
|
hub.users.RLock()
|
2017-09-03 04:50:31 +00:00
|
|
|
return len(hub.onlineUsers)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func (hub *WS_Hub) broadcastMessage(msg string) error {
|
2017-06-10 07:58:15 +00:00
|
|
|
hub.users.RLock()
|
2017-09-03 04:50:31 +00:00
|
|
|
for _, wsUser := range hub.onlineUsers {
|
|
|
|
w, err := wsUser.conn.NextWriter(websocket.TextMessage)
|
2017-06-10 07:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Write([]byte(msg))
|
|
|
|
}
|
|
|
|
hub.users.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func (hub *WS_Hub) pushMessage(targetUser int, msg string) error {
|
2017-06-10 07:58:15 +00:00
|
|
|
hub.users.RLock()
|
2017-09-03 04:50:31 +00:00
|
|
|
wsUser, ok := hub.onlineUsers[targetUser]
|
2017-06-10 07:58:15 +00:00
|
|
|
hub.users.RUnlock()
|
|
|
|
if !ok {
|
2017-09-03 04:50:31 +00:00
|
|
|
return errWsNouser
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
w, err := wsUser.conn.NextWriter(websocket.TextMessage)
|
2017-06-10 07:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(msg))
|
|
|
|
w.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func (hub *WS_Hub) pushAlert(targetUser int, asid int, event string, elementType string, actorID int, targetUser_id int, elementID int) error {
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("In push_alert")
|
2017-06-10 07:58:15 +00:00
|
|
|
hub.users.RLock()
|
2017-09-03 04:50:31 +00:00
|
|
|
wsUser, ok := hub.onlineUsers[targetUser]
|
2017-06-10 07:58:15 +00:00
|
|
|
hub.users.RUnlock()
|
|
|
|
if !ok {
|
2017-09-03 04:50:31 +00:00
|
|
|
return errWsNouser
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("Building alert")
|
2017-09-03 04:50:31 +00:00
|
|
|
alert, err := buildAlert(asid, event, elementType, actorID, targetUser_id, elementID, *wsUser.User)
|
2017-06-10 07:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("Getting WS Writer")
|
2017-09-03 04:50:31 +00:00
|
|
|
w, err := wsUser.conn.NextWriter(websocket.TextMessage)
|
2017-06-10 07:58:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(alert))
|
|
|
|
w.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func (hub *WS_Hub) pushAlerts(users []int, asid int, event string, elementType string, actorID int, targetUserID int, elementID int) error {
|
|
|
|
//log.Print("In pushAlerts")
|
|
|
|
var wsUsers []*WS_User
|
2017-06-12 09:03:14 +00:00
|
|
|
hub.users.RLock()
|
|
|
|
// We don't want to keep a lock on this for too long, so we'll accept some nil pointers
|
|
|
|
for _, uid := range users {
|
2017-09-03 04:50:31 +00:00
|
|
|
wsUsers = append(wsUsers, hub.onlineUsers[uid])
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
hub.users.RUnlock()
|
2017-09-03 04:50:31 +00:00
|
|
|
if len(wsUsers) == 0 {
|
|
|
|
return errWsNouser
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
2017-09-03 04:50:31 +00:00
|
|
|
for _, wsUser := range wsUsers {
|
|
|
|
if wsUser == nil {
|
2017-06-12 09:03:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("Building alert")
|
2017-09-03 04:50:31 +00:00
|
|
|
alert, err := buildAlert(asid, event, elementType, actorID, targetUserID, elementID, *wsUser.User)
|
2017-06-12 09:03:14 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
errs = append(errs, err)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("Getting WS Writer")
|
2017-09-03 04:50:31 +00:00
|
|
|
w, err := wsUser.conn.NextWriter(websocket.TextMessage)
|
2017-06-12 09:03:14 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
errs = append(errs, err)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(alert))
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the first error
|
|
|
|
if len(errs) != 0 {
|
|
|
|
for _, err := range errs {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
|
|
|
func route_websockets(w http.ResponseWriter, r *http.Request, user User) {
|
2017-09-03 04:50:31 +00:00
|
|
|
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
2017-05-11 13:04:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userptr, err := users.CascadeGet(user.ID)
|
|
|
|
if err != nil && err != ErrStoreCapacityOverflow {
|
|
|
|
return
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
wsUser := &WS_User{conn, userptr}
|
2017-05-11 13:04:43 +00:00
|
|
|
if user.ID == 0 {
|
2017-09-03 04:50:31 +00:00
|
|
|
wsHub.guests.Lock()
|
|
|
|
wsHub.onlineGuests[wsUser] = true
|
|
|
|
wsHub.guests.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
} else {
|
2017-09-03 04:50:31 +00:00
|
|
|
wsHub.users.Lock()
|
|
|
|
wsHub.onlineUsers[user.ID] = wsUser
|
|
|
|
wsHub.users.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
//conn.SetReadLimit(/* put the max request size from earlier here? */)
|
|
|
|
//conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
2017-09-03 04:50:31 +00:00
|
|
|
var currentPage []byte
|
2017-05-11 13:04:43 +00:00
|
|
|
for {
|
|
|
|
_, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if user.ID == 0 {
|
2017-09-03 04:50:31 +00:00
|
|
|
wsHub.guests.Lock()
|
|
|
|
delete(wsHub.onlineGuests, wsUser)
|
|
|
|
wsHub.guests.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
} else {
|
2017-09-03 04:50:31 +00:00
|
|
|
wsHub.users.Lock()
|
|
|
|
delete(wsHub.onlineUsers, user.ID)
|
|
|
|
wsHub.users.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("Message",message)
|
|
|
|
//log.Print("string(Message)",string(message))
|
2017-09-03 04:50:31 +00:00
|
|
|
messages := bytes.Split(message, []byte("\r"))
|
2017-05-11 13:04:43 +00:00
|
|
|
for _, msg := range messages {
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print("Submessage",msg)
|
|
|
|
//log.Print("Submessage",string(msg))
|
2017-09-03 04:50:31 +00:00
|
|
|
if bytes.HasPrefix(msg, []byte("page ")) {
|
|
|
|
msgblocks := bytes.SplitN(msg, []byte(" "), 2)
|
2017-05-11 13:04:43 +00:00
|
|
|
if len(msgblocks) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
if !bytes.Equal(msgblocks[1], currentPage) {
|
|
|
|
wsLeavePage(wsUser, currentPage)
|
|
|
|
currentPage = msgblocks[1]
|
|
|
|
//log.Print("Current Page:",currentPage)
|
|
|
|
//log.Print("Current Page:",string(currentPage))
|
|
|
|
wsPageResponses(wsUser, currentPage)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*if bytes.Equal(message,[]byte(`start-view`)) {
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
} else if bytes.Equal(message,[]byte(`end-view`)) {
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func wsPageResponses(wsUser *WS_User, page []byte) {
|
|
|
|
switch string(page) {
|
|
|
|
case "/panel/":
|
|
|
|
//log.Print("/panel/ WS Route")
|
|
|
|
/*w, err := wsUser.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if err != nil {
|
|
|
|
//log.Print(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
log.Print(wsHub.online_users)
|
|
|
|
uonline := wsHub.userCount()
|
|
|
|
gonline := wsHub.guestCount()
|
|
|
|
totonline := uonline + gonline
|
|
|
|
|
|
|
|
w.Write([]byte("set #dash-totonline " + strconv.Itoa(totonline) + " online\r"))
|
|
|
|
w.Write([]byte("set #dash-gonline " + strconv.Itoa(gonline) + " guests online\r"))
|
|
|
|
w.Write([]byte("set #dash-uonline " + strconv.Itoa(uonline) + " users online\r"))
|
|
|
|
w.Close()*/
|
|
|
|
|
|
|
|
// Listen for changes and inform the admins...
|
|
|
|
adminStatsMutex.Lock()
|
|
|
|
watchers := len(adminStatsWatchers)
|
|
|
|
adminStatsWatchers[wsUser] = true
|
|
|
|
if watchers == 0 {
|
|
|
|
go adminStatsTicker()
|
|
|
|
}
|
|
|
|
adminStatsMutex.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func wsLeavePage(wsUser *WS_User, page []byte) {
|
|
|
|
switch string(page) {
|
|
|
|
case "/panel/":
|
|
|
|
adminStatsMutex.Lock()
|
|
|
|
delete(adminStatsWatchers, wsUser)
|
|
|
|
adminStatsMutex.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
var adminStatsWatchers map[*WS_User]bool
|
|
|
|
var adminStatsMutex sync.RWMutex
|
|
|
|
|
|
|
|
func adminStatsTicker() {
|
2017-05-11 13:04:43 +00:00
|
|
|
time.Sleep(time.Second)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
var last_uonline int = -1
|
|
|
|
var last_gonline int = -1
|
|
|
|
var last_totonline int = -1
|
|
|
|
var last_cpu_perc int = -1
|
|
|
|
var last_available_ram int64 = -1
|
|
|
|
var no_stat_updates bool = false
|
2017-05-12 13:25:12 +00:00
|
|
|
var no_ram_updates bool = false
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
var onlineColour, onlineGuestsColour, onlineUsersColour, cpustr, cpuColour, ramstr, ramColour string
|
|
|
|
var cpuerr, ramerr error
|
|
|
|
var memres *mem.VirtualMemoryStat
|
|
|
|
var cpu_perc []float64
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
var totunit, uunit, gunit string
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
AdminStatLoop:
|
|
|
|
for {
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.RLock()
|
|
|
|
watch_count := len(adminStatsWatchers)
|
|
|
|
adminStatsMutex.RUnlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
if watch_count == 0 {
|
|
|
|
break AdminStatLoop
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
cpu_perc, cpuerr = cpu.Percent(time.Duration(time.Second), true)
|
2017-05-11 13:04:43 +00:00
|
|
|
memres, ramerr = mem.VirtualMemory()
|
2017-09-03 04:50:31 +00:00
|
|
|
uonline := wsHub.userCount()
|
|
|
|
gonline := wsHub.guestCount()
|
2017-05-11 13:04:43 +00:00
|
|
|
totonline := uonline + gonline
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
// It's far more likely that the CPU Usage will change than the other stats, so we'll optimise them seperately...
|
|
|
|
no_stat_updates = (uonline == last_uonline && gonline == last_gonline && totonline == last_totonline)
|
2017-05-12 13:25:12 +00:00
|
|
|
no_ram_updates = (last_available_ram == int64(memres.Available))
|
|
|
|
if int(cpu_perc[0]) == last_cpu_perc && no_stat_updates && no_ram_updates {
|
2017-05-11 13:04:43 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
if !no_stat_updates {
|
|
|
|
if totonline > 10 {
|
|
|
|
onlineColour = "stat_green"
|
|
|
|
} else if totonline > 3 {
|
|
|
|
onlineColour = "stat_orange"
|
|
|
|
} else {
|
|
|
|
onlineColour = "stat_red"
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
if gonline > 10 {
|
2017-09-03 04:50:31 +00:00
|
|
|
onlineGuestsColour = "stat_green"
|
2017-05-11 13:04:43 +00:00
|
|
|
} else if gonline > 1 {
|
|
|
|
onlineGuestsColour = "stat_orange"
|
|
|
|
} else {
|
|
|
|
onlineGuestsColour = "stat_red"
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
if uonline > 5 {
|
|
|
|
onlineUsersColour = "stat_green"
|
|
|
|
} else if uonline > 1 {
|
|
|
|
onlineUsersColour = "stat_orange"
|
|
|
|
} else {
|
|
|
|
onlineUsersColour = "stat_red"
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
totonline, totunit = convertFriendlyUnit(totonline)
|
|
|
|
uonline, uunit = convertFriendlyUnit(uonline)
|
|
|
|
gonline, gunit = convertFriendlyUnit(gonline)
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
if cpuerr != nil {
|
|
|
|
cpustr = "Unknown"
|
|
|
|
} else {
|
|
|
|
calcperc := int(cpu_perc[0]) / runtime.NumCPU()
|
|
|
|
cpustr = strconv.Itoa(calcperc)
|
|
|
|
if calcperc < 30 {
|
|
|
|
cpuColour = "stat_green"
|
|
|
|
} else if calcperc < 75 {
|
|
|
|
cpuColour = "stat_orange"
|
|
|
|
} else {
|
|
|
|
cpuColour = "stat_red"
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
if !no_ram_updates {
|
|
|
|
if ramerr != nil {
|
|
|
|
ramstr = "Unknown"
|
2017-05-11 13:04:43 +00:00
|
|
|
} else {
|
2017-09-03 04:50:31 +00:00
|
|
|
total_count, total_unit := convertByteUnit(float64(memres.Total))
|
|
|
|
used_count := convertByteInUnit(float64(memres.Total-memres.Available), total_unit)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
// Round totals with .9s up, it's how most people see it anyway. Floats are notoriously imprecise, so do it off 0.85
|
|
|
|
var totstr string
|
|
|
|
if (total_count - float64(int(total_count))) > 0.85 {
|
|
|
|
used_count += 1.0 - (total_count - float64(int(total_count)))
|
|
|
|
totstr = strconv.Itoa(int(total_count) + 1)
|
|
|
|
} else {
|
2017-09-03 04:50:31 +00:00
|
|
|
totstr = fmt.Sprintf("%.1f", total_count)
|
2017-05-12 13:25:12 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
if used_count > total_count {
|
|
|
|
used_count = total_count
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
ramstr = fmt.Sprintf("%.1f", used_count) + " / " + totstr + total_unit
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
ramperc := ((memres.Total - memres.Available) * 100) / memres.Total
|
|
|
|
if ramperc < 50 {
|
|
|
|
ramColour = "stat_green"
|
|
|
|
} else if ramperc < 75 {
|
|
|
|
ramColour = "stat_orange"
|
|
|
|
} else {
|
|
|
|
ramColour = "stat_red"
|
|
|
|
}
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.RLock()
|
|
|
|
watchers := adminStatsWatchers
|
|
|
|
adminStatsMutex.RUnlock()
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
for watcher, _ := range watchers {
|
|
|
|
w, err := watcher.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if err != nil {
|
2017-08-13 11:22:34 +00:00
|
|
|
//log.Print(err.Error())
|
2017-09-03 04:50:31 +00:00
|
|
|
adminStatsMutex.Lock()
|
|
|
|
delete(adminStatsWatchers, watcher)
|
|
|
|
adminStatsMutex.Unlock()
|
2017-05-11 13:04:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
if !no_stat_updates {
|
2017-05-12 13:25:12 +00:00
|
|
|
w.Write([]byte("set #dash-totonline " + strconv.Itoa(totonline) + totunit + " online\r"))
|
|
|
|
w.Write([]byte("set #dash-gonline " + strconv.Itoa(gonline) + gunit + " guests online\r"))
|
|
|
|
w.Write([]byte("set #dash-uonline " + strconv.Itoa(uonline) + uunit + " users online\r"))
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
w.Write([]byte("set-class #dash-totonline grid_item grid_stat " + onlineColour + "\r"))
|
|
|
|
w.Write([]byte("set-class #dash-gonline grid_item grid_stat " + onlineGuestsColour + "\r"))
|
|
|
|
w.Write([]byte("set-class #dash-uonline grid_item grid_stat " + onlineUsersColour + "\r"))
|
2017-05-11 13:04:43 +00:00
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
w.Write([]byte("set #dash-cpu CPU: " + cpustr + "%\r"))
|
2017-05-12 13:25:12 +00:00
|
|
|
w.Write([]byte("set-class #dash-cpu grid_item grid_istat " + cpuColour + "\r"))
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-12 13:25:12 +00:00
|
|
|
if !no_ram_updates {
|
|
|
|
w.Write([]byte("set #dash-ram RAM: " + ramstr + "\r"))
|
|
|
|
w.Write([]byte("set-class #dash-ram grid_item grid_istat " + ramColour + "\r"))
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
w.Close()
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
last_uonline = uonline
|
|
|
|
last_gonline = gonline
|
|
|
|
last_totonline = totonline
|
|
|
|
last_cpu_perc = int(cpu_perc[0])
|
|
|
|
last_available_ram = int64(memres.Available)
|
2017-06-10 07:58:15 +00:00
|
|
|
|
2017-05-11 13:04:43 +00:00
|
|
|
//time.Sleep(time.Second)
|
|
|
|
}
|
2017-06-10 07:58:15 +00:00
|
|
|
}
|