Guest User

sysinfo

a guest
Aug 6th, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #! /usr/bin/tclsh
  2.  
  3. # sysinfo.tcl - by FireEgl - May 2011
  4.  
  5. # pub command !sysinfo reads some files in /proc/ and /etc/ and returns some basic system info.
  6.  
  7. # Note, you must .chanset #Channel +sysinfo
  8. # before the script will work on a channel.
  9.  
  10. namespace eval ::sysinfo {
  11. variable pubcmd !sysinfo
  12. # Shows everything:
  13. variable format {Hostname: [hostname], Distribution: [distrib], OS: $::tcl_platform(os) $::tcl_platform(osVersion)/$::tcl_platform(machine), CPU: [cpu], Load Average: [loadavg], Processes: [processes], Memory Used: [memory], Users: [users], System Uptime: [uptime], Bot Uptime: [botuptime], On IRC: [onlinetime].}
  14. # For grsecurity with proc restrictions enabled:
  15. #variable format {Hostname: [info hostname], Distribution: [distrib], OS: $::tcl_platform(os) $::tcl_platform(osVersion)/$::tcl_platform(machine), CPU: [cpu], Load Average: [loadavg], Processes: [processes], Memory Used: [memory], System Uptime: [uptime], Bot Uptime: [botuptime], On IRC: [onlinetime].}
  16. # For Cygwin:
  17. #variable format {Hostname: [info hostname], OS: $::tcl_platform(os) $::tcl_platform(osVersion)/$::tcl_platform(machine), CPU: [cpu], Processes: [processes], System Uptime: [uptime], Bot Uptime: [botuptime], On IRC: [onlinetime].}
  18. }
  19.  
  20. proc ::sysinfo::sysinfo {{default {Problem getting sysinfo.}}} {
  21. variable format
  22. if {![catch { subst $format } out]} {
  23. return $out
  24. } else {
  25. return $default
  26. }
  27. }
  28. proc ::sysinfo::cpu {{default {Unknown}}} {
  29. if {[set cpuinfo [readfile /proc/cpuinfo]] ne {} && [regexp -line -- {model name\s*:\s*(.*)} $cpuinfo -> modelname] && [regexp -line -- {cpu MHz\s*:\s*(.*)} $cpuinfo -> cpumhz] && [set processors [regexp -all -line -- {processor\s*:.*} $cpuinfo]]} {
  30. return "${processors}x [string map [list {(R)} "\u00AE" {(TM)} "\u2122" {(C)} "\u00A9" { } { } { } { } { } { } { } { }] $modelname] ([format %.0f $cpumhz]MHz)"
  31. } else {
  32. return $default
  33. }
  34. }
  35. proc ::sysinfo::loadavg {{default {0}}} {
  36. if {[set loadavg [readfile /proc/loadavg]] ne {}} {
  37. lindex $loadavg 0
  38. } else {
  39. return $default
  40. }
  41. }
  42. proc ::sysinfo::uptime {{default {Unknown}}} {
  43. if {[set uptime [readfile /proc/uptime]] ne {}} {
  44. secstodays [lindex $uptime 0]
  45. } else {
  46. return $default
  47. }
  48. }
  49. proc ::sysinfo::distrib {{default {Unknown}}} {
  50. if {[set lsbinfo [readfile /etc/lsb-release]] ne {}} {
  51. set lsbdesc $default
  52. regexp -line -- {DISTRIB_DESCRIPTION="(.*)"} $lsbinfo -> lsbdesc
  53. return $lsbdesc
  54. } elseif {[set lsbdesc [readfile /etc/debian_version]] ne {}} {
  55. return "Debian $lsbdesc"
  56. } else {
  57. return $default
  58. }
  59. }
  60. proc ::sysinfo::memory {{default {?}}} {
  61. if {[set meminfo [readfile /proc/meminfo]] ne {} && [regexp -line -- {MemTotal:\s*(\d+) kB} $meminfo -> memtotal] && [regexp -line -- {MemFree:\s*(\d+) kB} $meminfo -> memfree]} {
  62. set memused [expr { $memtotal - $memfree }]
  63. return "[format %.0f [expr { $memused / 1024.0 }]]MB/[format %.0f [expr { $memtotal / 1024.0 }]]MB"
  64. } else {
  65. return $default
  66. }
  67. }
  68. proc ::sysinfo::users {{default {?}}} {
  69. if {![catch { exec who -q } who] && [regexp {# users=(\d)} $who -> count]} {
  70. return $count
  71. } elseif {![catch { llength [split [exec w -h] \n] } count]} {
  72. return $count
  73. } elseif {[file exists /proc/consoles]} {
  74. # Note: I think /proc/consoles only shows local logins..
  75. llength [split [readfile /proc/consoles] \n]
  76. } else {
  77. return $default
  78. }
  79. }
  80. proc ::sysinfo::processes {{default {0}}} {
  81. if {[set processes [lindex [readfile /proc/loadavg] 3]] ne {}} {
  82. return "$processes (running/total)"
  83. } else {
  84. llength [glob -directory /proc/ -tails -nocomplain 1* 2* 3* 4* 5* 6* 7* 8* 9*]
  85. }
  86. }
  87. proc ::sysinfo::hostname {{default {?}}} { info hostname }
  88. proc ::sysinfo::os {{default {?}}} { return $::tcl_platform(os) }
  89. proc ::sysinfo::osver {{default {?}}} { return $::tcl_platform(osVersion) }
  90. proc ::sysinfo::machine {{default {?}}} { return $::tcl_platform(machine) }
  91. proc ::sysinfo::readfile {file {default {}}} {
  92. if {![catch { read -nonewline [set fid [open $file r]] } out]} {
  93. close $fid
  94. return $out
  95. } else {
  96. return $default
  97. }
  98. }
  99. proc ::sysinfo::onlinetime {{default {?}}} { secstodays [expr { [clock seconds] - ${::server-online} }] }
  100. proc ::sysinfo::botuptime {{default {?}}} { secstodays [expr { [clock seconds] - $::uptime }] }
  101. proc ::sysinfo::secstodays {seconds args} { return "[expr { [format %.0f $seconds] / 86400 }] days" }
  102.  
  103. if {[info exists ::numversion] && [llength [info commands bind]] && ![catch { bind pub - $::sysinfo::pubcmd ::sysinfo::PUB }]} {
  104. # This part is for running on Eggdrop.
  105. proc ::sysinfo::PUB {nick host hand chan text} {
  106. if {[channel get $chan sysinfo] && ([string trim $text] eq {} || [isbotnick $text])} {
  107. puthelp [encoding convertto utf-8 "PRIVMSG $chan :[sysinfo]"]
  108. }
  109. }
  110. setudef flag sysinfo
  111. putlog "sysinfo.tcl - Loaded."
  112. } else {
  113. # This part is for running from the command-line.
  114. puts [::sysinfo::sysinfo]
  115. }
Advertisement
Add Comment
Please, Sign In to add comment