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.

249 lines
5.4 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
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. data = [i.split(None, 2) for i in data]
  54. for e in data:
  55. if len(e) > 2:
  56. itf = dict(zip([iter(e[0])]))
  57. else:
  58. itf = [e[0]]
  59. ips = {'interface': itf, 'itfip': data}
  60. data = ips
  61. except Exception,err:
  62. data = str(err)
  63. return data
  64. def get_cpus():
  65. """
  66. Get the number of CPUs and model/type
  67. """
  68. try:
  69. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  70. data = pipe.read().strip().split(':',2)[-1]
  71. pipe.close()
  72. cpus = multiprocessing.cpu_count()
  73. data = {'cpus': cpus, 'type': data}
  74. except Exception, err:
  75. data = str(err)
  76. return data
  77. def get_users():
  78. """
  79. Get the current logged in users
  80. """
  81. try:
  82. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  83. data = pipe.read().strip().split('\n')
  84. pipe.close()
  85. data = [i.split(None, 3) for i in data]
  86. except Exception, err:
  87. data = str(err)
  88. if data[0] == []:
  89. data = [['No', 'data', 'available']]
  90. return data
  91. def get_traffic(request):
  92. """
  93. Get the traffic for the specified interface
  94. """
  95. try:
  96. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  97. data = pipe.read().strip().split(':',1)[-1]
  98. pipe.close()
  99. data = data.split()
  100. traffic_in = int(data[0])
  101. traffic_out = int(data[1])
  102. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  103. data = all_traffic
  104. except Exception,err:
  105. data = str(err)
  106. return data
  107. def get_platform():
  108. """
  109. Get the OS name
  110. """
  111. try:
  112. data = " ".join(platform.linux_distribution())
  113. except Exception,err:
  114. data = str(err)
  115. return data
  116. def get_disk():
  117. """
  118. Get disk usage
  119. """
  120. try:
  121. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  122. data = pipe.read().strip().split('\n')
  123. pipe.close()
  124. data = [i.split(None, 6) for i in data]
  125. except Exception,err:
  126. data = str(err)
  127. return data
  128. def get_mem():
  129. try:
  130. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  131. data = pipe.read().strip().split()
  132. pipe.close()
  133. allmem = int(data[0])
  134. freemem = int(data[1])
  135. percent = (100 - ((freemem * 100) / allmem))
  136. usage = (allmem - freemem)
  137. mem_usage = {'usage': usage, 'percent': percent}
  138. data = mem_usage
  139. except Exception,err:
  140. data = str(err)
  141. return data
  142. def get_cpu_usage():
  143. try:
  144. pipe = os.popen("ps aux --sort -%cpu,-rss")
  145. data = pipe.read().strip().split('\n')
  146. pipe.close()
  147. usage = [i.split(None, 10) for i in data]
  148. del usage[0]
  149. total_usage = []
  150. for element in usage:
  151. usage_cpu = element[2]
  152. total_usage.append(usage_cpu)
  153. #del total_usage[0]
  154. total_usage = sum(float(i) for i in total_usage)
  155. total_free = (100 - float(total_usage))
  156. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  157. data = cpu_used
  158. except Exception,err:
  159. data = str(err)
  160. return data
  161. def get_load():
  162. try:
  163. data = os.getloadavg()[0]
  164. except Exception, err:
  165. data = str(err)
  166. return data
  167. def getall(request):
  168. if not request.user.is_authenticated():
  169. return HttpResponseRedirect('/login')
  170. return render_to_response('main.html', {#'getuptime': get_uptime(),
  171. 'gethostname': get_hostname(),
  172. 'getplatform': get_platform(),
  173. 'getcpus': get_cpus(),
  174. #'getdisk': get_disk(),
  175. #'getip': get_ipaddress(),
  176. #'gettraffic': get_traffic('eth0'),
  177. #'getusers': get_users(),
  178. #'getcpuusage': get_cpu_usage(),
  179. 'time_refresh': time_refresh,
  180. 'time_refresh_long': time_refresh_long,
  181. 'time_refresh_net': time_refresh_net
  182. }, context_instance=RequestContext(request))