You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
5.1 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. import socket, platform, os, multiprocessing
  2. from datetime import timedelta
  3. from django.core import serializers
  4. from django.shortcuts import render_to_response
  5. from django.http import HttpResponseRedirect, HttpResponse
  6. from django.template import RequestContext
  7. from django.utils.translation import ugettext_lazy as _
  8. from django.utils import simplejson
  9. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG
  10. time_refresh = TIME_JS_REFRESH
  11. time_refresh_long = TIME_JS_REFRESH_LONG
  12. def index(request):
  13. """
  14. Index page.
  15. """
  16. if not request.user.is_authenticated():
  17. return HttpResponseRedirect('/login')
  18. else:
  19. return HttpResponseRedirect('/main')
  20. def chunks(get, n):
  21. return [get[i:i+n] for i in range(0, len(get), n)]
  22. def get_uptime():
  23. """
  24. Get uptime
  25. """
  26. try:
  27. with open('/proc/uptime', 'r') as f:
  28. uptime_seconds = float(f.readline().split()[0])
  29. uptime_time = str(timedelta(seconds = uptime_seconds))
  30. data = uptime_time.split('.', 1)[0]
  31. except Exception,err:
  32. data = str(err)
  33. return data
  34. def get_hostname():
  35. """
  36. Get the hostname
  37. """
  38. try:
  39. data = socket.gethostname()
  40. except Exception,err:
  41. data = str(err)
  42. return data
  43. def get_ipaddress():
  44. """
  45. Get the IP Address
  46. """
  47. try:
  48. pipe = os.popen("/sbin/ifconfig |" + "grep -B1 'inet addr' |" + "awk '{ if ( $1 == \"inet\" ) { print $2 } else if ( $2 == \"Link\" ) { printf \"%s:\",$1 } }' |" + "awk -F: '{ print $1, $3 }'")
  49. data = pipe.read().strip().split()
  50. pipe.close()
  51. data = [n for n in data if not n.startswith(('lo', '127'))]
  52. itf = dict(zip(*[iter(data)] * 2))
  53. ips = {'interface': itf, 'itfip': data}
  54. data = ips
  55. except Exception,err:
  56. data = str(err)
  57. return data
  58. def get_cpus():
  59. """
  60. Get the number of CPUs and model/type
  61. """
  62. try:
  63. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  64. data = pipe.read().strip().split(':',2)[-1]
  65. pipe.close()
  66. cpus = multiprocessing.cpu_count()
  67. data = {'cpus': cpus, 'type': data}
  68. except Exception, err:
  69. data = str(err)
  70. return data
  71. def get_users():
  72. """
  73. Get the current logged in users
  74. """
  75. try:
  76. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  77. data = pipe.read().strip().split('\n')
  78. pipe.close()
  79. data = [i.split(None, 3) for i in data]
  80. except Exception, err:
  81. data = str(err)
  82. if data[0] == []:
  83. data = [['No', 'data', 'available']]
  84. return data
  85. def get_traffic(request):
  86. """
  87. Get the traffic for the specified interface
  88. """
  89. try:
  90. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk {'print $1, $9'}")
  91. data = pipe.read().strip().split(':',1)[-1]
  92. pipe.close()
  93. data = data.split()
  94. traffic_in = int(data[0])
  95. traffic_out = int(data[1])
  96. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  97. data = all_traffic
  98. except Exception,err:
  99. data = str(err)
  100. return data
  101. def get_platform():
  102. """
  103. Get the OS name
  104. """
  105. try:
  106. data = " ".join(platform.linux_distribution())
  107. except Exception,err:
  108. data = str(err)
  109. return data
  110. def get_disk():
  111. """
  112. Get disk usage
  113. """
  114. try:
  115. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  116. data = pipe.read().strip().split('\n')
  117. pipe.close()
  118. data = [i.split(None, 6) for i in data]
  119. except Exception,err:
  120. data = str(err)
  121. return data
  122. def get_mem():
  123. try:
  124. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  125. data = pipe.read().strip().split()
  126. pipe.close()
  127. allmem = int(data[0])
  128. freemem = int(data[1])
  129. percent = (100 - ((freemem * 100) / allmem))
  130. usage = (allmem - freemem)
  131. mem_usage = {'usage': usage, 'percent': percent}
  132. data = mem_usage
  133. except Exception,err:
  134. data = str(err)
  135. return data
  136. def get_cpu_usage():
  137. try:
  138. pipe = os.popen("ps aux --sort -%cpu,-rss")
  139. data = pipe.read().strip().split('\n')
  140. pipe.close()
  141. usage = [i.split(None, 10) for i in data]
  142. del usage[0]
  143. total_usage = []
  144. for element in usage:
  145. usage_cpu = element[2]
  146. total_usage.append(usage_cpu)
  147. #del total_usage[0]
  148. total_usage = sum(float(i) for i in total_usage)
  149. total_free = (100 - float(total_usage))
  150. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  151. data = cpu_used
  152. except Exception,err:
  153. data = str(err)
  154. return data
  155. def get_load():
  156. try:
  157. data = os.getloadavg()[0]
  158. except Exception, err:
  159. data = str(err)
  160. return data
  161. def getall(request):
  162. if not request.user.is_authenticated():
  163. return HttpResponseRedirect('/login')
  164. return render_to_response('main.html', {'getuptime': get_uptime(),
  165. 'gethostname': get_hostname(),
  166. 'getplatform': get_platform(),
  167. 'getcpus': get_cpus(),
  168. #'getdisk': get_disk(),
  169. 'getip': get_ipaddress(),
  170. 'gettraffic': get_traffic('eth0'),
  171. #'getusers': get_users(),
  172. 'getcpuusage': get_cpu_usage(),
  173. 'time_refresh': time_refresh,
  174. 'time_refresh_long': time_refresh_long
  175. }, context_instance=RequestContext(request))