Author Topic: Server Uptime  (Read 7001 times)

0 Members and 1 Guest are viewing this topic.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Server Uptime
« on: January 20, 2007, 04:56:32 PM »
At first, I thought an 'uptime' for my server would be easy. Using Garry's Mod v10 lua command string.FormattedTime (found in his lua\extensions\string.lua)
However, I found it had two things wrong.
1) First and foremost, it displays the wrong time after more than 24 hours.
2) It didn't display days. I'd prefer days instead of milliseconds.

So, I wrote my own. Feel free to use this or any derivative of this in any code you wish.
Rewrite it if you wish to be able to pass your own time to it. It wouldn't be difficult to make it exactly like what string.FormattedTime was supposed to be.
As it is, it will use the time the server has been running.

Passing the function "full" will result in "# weeks, # days, # hours, # minutes, # seconds". "short" (or any other word) will result in "0#:0#:0#:0#:0# (w:d:h:m:s)"

Code: [Select]
function Umotd_GetUptime(fmt) -- Credit to Garry. Idea from lua\extensions\string.lua "FormattedTime" function. Though, his math ends up wrong at times.
          local U_time = math.floor(os.clock{})
          local w, d, h, m, s = 0,0,0,0,0
                w = math.floor(U_time/604800)
                U_time = U_time - (w*604800)
                d = math.floor(U_time/86400)
                U_time = U_time - (d*86400)
                h = math.floor( U_time/3600 )
                U_time = U_time - (h*3600)
                m = math.floor( U_time/60 )
                U_time = U_time - (m*60)
                s = U_time
          if fmt ~= "full" then return string.format("%02i:%02i:%02i:%02i:%02i",w,d,h,m,s) .. " (wk:d:hr:min:sec)"
          else return w.. " weeks, " ..d .. " days, " .. h .. " hours, " .. m .. " minutes, " .. s .. " seconds."
          end
 end
« Last Edit: January 21, 2007, 06:21:50 AM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Server Uptime
« Reply #1 on: January 20, 2007, 07:59:05 PM »
Seems even though I tested various times, including more than 24 hours, when I actually run it on my server, after more than one day, even my math goes bad.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Server Uptime
« Reply #2 on: January 21, 2007, 06:20:26 AM »
There. Fixed it. Much simpler too. Why I didn't think of this originally, I have no idea.
So simple, i threw in weeks. A weeks is 7 days. No leap year calculations. Just 7 days.
If you want months, you'll have to figure out the code yourself and add it. I didn't want to figure out leap years, which month, etc. :D
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline monkeynutts

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Server Uptime
« Reply #3 on: July 06, 2007, 05:12:40 PM »
So where to I place this code on my server to impliment it?

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Server Uptime
« Reply #4 on: July 06, 2007, 08:17:50 PM »
So where to I place this code on my server to impliment it?

You could place it in lua/autorun as any_filename_you_wanted.lua along with the extra code needed to output text to somewhere.
In its current state, it is only a function that could be used in other code, prints no output, and is not a full release.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Bryantdl7

  • Jr. Member
  • **
  • Posts: 86
  • Karma: -2
Re: Server Uptime
« Reply #5 on: August 24, 2014, 11:04:42 AM »
I feel like an idiot bumping a 7 yr old post... Does this still work? I am trying to find some sort of addon to monitor my server's uptime better than the cvar "stats"

I have a feeling it doesn't but it can't hurt to ask.
« Last Edit: August 24, 2014, 11:07:43 AM by Bryantdl7 »



Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Server Uptime
« Reply #6 on: August 24, 2014, 02:12:31 PM »
Oddly enough, I don't see why it wouldn't work- most of the code used here is basic lua functions- nothing strictly Garrysmod related. Try it and see! :P

Edit: Holy crap, has it been 7 years?!?
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Server Uptime
« Reply #7 on: August 24, 2014, 03:22:58 PM »
Code: [Select]
concommand.Add('uptime', function(ply)
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)

This only works from the server console, but it should be easy enough to modify it to work for players in game. The check is already there. Just add code where it says -- TODO
« Last Edit: August 24, 2014, 03:25:29 PM by MrPresident »

Offline Cobalt

  • Full Member
  • ***
  • Posts: 216
  • Karma: 44
  • http://steamcommunity.com/id/__yvl/
Re: Server Uptime
« Reply #8 on: August 24, 2014, 06:20:35 PM »
Code: [Select]
concommand.Add('uptime', function(ply)
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)

This only works from the server console, but it should be easy enough to modify it to work for players in game. The check is already there. Just add code where it says -- TODO
I would use SysTime instead of CurTime because it isn't affected by game events such as the timescale changing.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Server Uptime
« Reply #9 on: August 24, 2014, 07:05:39 PM »
Good call, never affected me because we don't mess with the timescale, but some people might.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Server Uptime
« Reply #10 on: August 25, 2014, 01:43:36 PM »
For once, this is one zombie thread I find impressive. Like a good wine, better with age, and improved by the community.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Bryantdl7

  • Jr. Member
  • **
  • Posts: 86
  • Karma: -2
Re: Server Uptime
« Reply #11 on: October 28, 2015, 12:31:23 PM »
Code: [Select]
concommand.Add('uptime', function(ply)
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)

This only works from the server console, but it should be easy enough to modify it to work for players in game. The check is already there. Just add code where it says -- TODO


One year, 2.8 months later; does this look right? 

Code: [Select]
local uptime = ulx.command( "Utility", "ulx uptime", ulx.uptime, "!uptime" )
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
ulx.fancyLogAdmin( calling_ply, "The Server uptime is"..str)
-- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)