uptime.c
changeset 9 c1f0277182a3
parent 8 af9ebb57baf6
child 10 36e7f4269583
equal deleted inserted replaced
8:af9ebb57baf6 9:c1f0277182a3
    97 }
    97 }
    98 
    98 
    99 void do_uptime (char *arg)
    99 void do_uptime (char *arg)
   100 {
   100 {
   101 	GString *line = g_string_new (NULL);
   101 	GString *line = g_string_new (NULL);
   102 	guint seconds;
   102 	gchar strstartdate[256];
   103 	guint minutes;
   103 
   104 	guint hours;
   104 	guint seconds = time (NULL) - starttime;
   105 	guint days;
   105 	guint minutes = seconds / 60;
   106 	time_t now = time (NULL);
   106 	guint hours   = minutes / 60;
   107 
   107 	guint days    = hours   / 24;
   108 	if (proc_used && settings_opt_get_int ("uptime_use_proc")) {
   108 
   109 		int sysup = sys_uptime();
       
   110 		if (!sysup) {
       
   111 			g_string_free (line, TRUE);
       
   112 			scr_log_print (LPRINT_NORMAL, "Uptime not available :-(");
       
   113 			return;
       
   114 		}
       
   115 		seconds = (sysup - mstime) / hz;
       
   116 	} else {
       
   117 		seconds = now - starttime;
       
   118 	}
       
   119 
       
   120 	minutes = seconds / 60;
       
   121 	hours   = minutes / 60;
       
   122 	days    = hours   / 24;
       
   123 	seconds %= 60;
   109 	seconds %= 60;
   124 	minutes %= 60;
   110 	minutes %= 60;
   125 	hours   %= 24;
   111 	hours   %= 24;
   126 
   112 
   127 	if (days)
   113 	if (days)
   131 	if (minutes)
   117 	if (minutes)
   132 		g_string_append_printf (line, " %u minutes", minutes);
   118 		g_string_append_printf (line, " %u minutes", minutes);
   133 	if (seconds)
   119 	if (seconds)
   134 		g_string_append_printf (line, " %u seconds", seconds);
   120 		g_string_append_printf (line, " %u seconds", seconds);
   135 
   121 
   136 	scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds");
   122 	// Running since:
       
   123 	strftime(strstartdate, sizeof(strstartdate),
       
   124 			"%Y-%m-%d %H:%M", localtime(&starttime));
       
   125 
       
   126 	scr_log_print (LPRINT_NORMAL, "Uptime: %s.\n(Running since %s)",
       
   127 			line -> len ? line -> str : " 0 second",
       
   128 			strstartdate);
   137 
   129 
   138 	g_string_free (line, TRUE);
   130 	g_string_free (line, TRUE);
   139 }
   131 }
   140 
   132 
   141 gchar *g_module_check_init (GModule *module)
   133 gchar *g_module_check_init (GModule *module)
   220 			return "Malformed own start time.";
   212 			return "Malformed own start time.";
   221 		}
   213 		}
   222 		g_string_free (line, TRUE);
   214 		g_string_free (line, TRUE);
   223 
   215 
   224 		hz = sysconf(_SC_CLK_TCK);
   216 		hz = sysconf(_SC_CLK_TCK);
   225 		if (hz && mstime)
   217 		if (hz && mstime) {
       
   218 			int sysup = sys_uptime();
       
   219 			if (sysup) // Let's calculate starttime more precisely:
       
   220 				starttime = time (NULL) - (sysup - mstime) / hz;
   226 			proc_used = TRUE;
   221 			proc_used = TRUE;
       
   222 		}
   227 	}
   223 	}
   228 
   224 
   229 	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);
   225 	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);
   230 	return NULL;
   226 	return NULL;
   231 }
   227 }