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.

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