MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | #pragma strong_types, save_types |
| 2 | #pragma no_clone, no_shadow |
| 3 | |
| 4 | #include "/p/daemon/ch.h" |
| 5 | #include "/p/daemon/uptime_master.h" |
bugfix | 4fcca73 | 2020-03-23 17:04:14 +0100 | [diff] [blame] | 6 | #include <config.h> |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 7 | |
| 8 | /// Prototypen /// |
| 9 | |
| 10 | void create(); |
| 11 | void reset(); |
| 12 | |
| 13 | void SaveMaxUptime(); |
| 14 | mapping QuerMaxUptime(); |
| 15 | |
| 16 | /// Globale Variablen /// |
| 17 | static int next_save_uptime; |
| 18 | static int max_uptime_reached; |
| 19 | static int last_record_boot; |
| 20 | |
| 21 | /// Funktionen /// |
| 22 | |
| 23 | void create() |
| 24 | { |
| 25 | seteuid(getuid(this_object())); |
| 26 | reset(); |
| 27 | } |
| 28 | |
| 29 | void reset() |
| 30 | { |
| 31 | call_out("SaveMaxUptime",1); |
| 32 | set_next_reset(300); |
| 33 | } |
| 34 | |
| 35 | // Ist eine Rekord-Uptime erreicht, wird diese hier gespeichert. |
| 36 | void SaveMaxUptime() |
| 37 | { |
| 38 | int max_shut, max_boot; |
| 39 | |
| 40 | // Lohnt es, schon wieder zu pruefen? |
| 41 | if (time()>next_save_uptime) |
| 42 | { |
| 43 | // Initialisieren |
| 44 | if (next_save_uptime==0) |
| 45 | { |
| 46 | DEBUG("Masterinit"); |
| 47 | |
| 48 | // Zeit berechnen, ab der ein Rekord aufgestellt ist. |
| 49 | // Vorher braucht nixhts mehr geprueft werden |
| 50 | sscanf(read_file(MAX_UPTIME)||"0 0","%d %d",last_record_boot, max_shut); |
| 51 | next_save_uptime=last_reboot_time()+(max_shut-last_record_boot)+1; |
| 52 | |
| 53 | } else { |
| 54 | DEBUG("Rekord neu speichern."); |
| 55 | |
| 56 | // Neuen Rekord in Datei schreiben. |
| 57 | catch(rm(MAX_UPTIME);publish); |
| 58 | write_file(MAX_UPTIME, last_reboot_time()+" "+time()); |
| 59 | |
| 60 | // Jetzt checken, ob die Uptime grade geknackt wurde ... |
| 61 | // Wenn ja, dann eine Meldung an den Master |
| 62 | if(last_reboot_time()!=last_record_boot) |
| 63 | { |
| 64 | DEBUG("Rekordmeldung. MB:"+last_record_boot+" LU:"+last_reboot_time()); |
| 65 | |
| 66 | // Nur einmal pro Rekord Uptime melden ... |
| 67 | last_record_boot=last_reboot_time(); |
| 68 | |
| 69 | // Hurraaah :) |
| 70 | CHMASTER->join(RECORD_CHANNEL,this_object()); |
| 71 | CHMASTER->send(RECORD_CHANNEL,this_object(), |
| 72 | "[<MasteR>:Lars] Wow, eine Rekord-Uptime.\n" |
| 73 | "[<MasteR>:Merlin] Das erlebt man auch nicht alle Tage ...\n" |
| 74 | "[<MasteR>:Der GameDriver] Dann wird es wohl Zeit fuer einen Reboot, oder?\n" |
| 75 | "[<MasteR>:Merlin] Faules Stueck, arbeite weiter!\n" |
| 76 | "[<MasteR>:Der GameDriver] Och menno!", |
| 77 | MSG_EMPTY); |
| 78 | CHMASTER->leave(RECORD_CHANNEL,this_object()); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Gibt Daten zur bisher maximalen Uptime zurueck: (299 Ticks) |
| 85 | // 1.) Wann wurde gebootet |
| 86 | // 2.) Wann endete die Uptime |
| 87 | // 3.) Wann wird dieser Rekord gebrochen? |
| 88 | // 4.) Dauer der Uptime als lesbarer String. |
| 89 | mapping QueryMaxUptime() |
| 90 | { |
| 91 | int t, tmp, max_boot, max_shut; |
| 92 | string s; |
| 93 | |
| 94 | sscanf(read_file(MAX_UPTIME)||"0 0","%d %d",max_boot, max_shut); |
| 95 | |
| 96 | t=max_shut-max_boot; |
| 97 | |
| 98 | s=""; |
| 99 | if (t>=86400) |
| 100 | s+=sprintf("%d Tag%s, ",tmp=t/86400,(tmp==1?"":"e")); |
| 101 | if (t>=3600) |
| 102 | s+=sprintf("%d Stunde%s, ",tmp=(t=t%86400)/3600,(tmp==1?"":"n")); |
| 103 | if (t>60) |
| 104 | s+=sprintf("%d Minute%s und ",tmp=(t=t%3600)/60,(tmp==1?"":"n")); |
| 105 | s+=sprintf("%d Sekunde%s",t=t%60,(t==1?"":"n")); |
| 106 | |
| 107 | return (["beginn":max_boot, |
| 108 | "ende":max_shut, |
| 109 | "naechster rekord":last_reboot_time()+(max_shut-max_boot), |
| 110 | "rekord als string":s]); |
| 111 | } |