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.

258 lines
5.5 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
12 years ago
12 years ago
  1. import socket, platform, os, multiprocessing, json
  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 pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION
  9. time_refresh = TIME_JS_REFRESH
  10. time_refresh_long = TIME_JS_REFRESH_LONG
  11. time_refresh_net = TIME_JS_REFRESH_NET
  12. version = VERSION
  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. if type(data[0]) is str:
  100. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  101. data = pipe.read().strip().split(':',1)[-1]
  102. pipe.close()
  103. data = data.split()
  104. traffic_in = int(data[0])
  105. traffic_out = int(data[1])
  106. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  107. data = all_traffic
  108. except Exception,err:
  109. data = str(err)
  110. return data
  111. def get_platform():
  112. """
  113. Get the OS name
  114. """
  115. try:
  116. data = " ".join(platform.linux_distribution())
  117. except Exception,err:
  118. data = str(err)
  119. return data
  120. def get_disk():
  121. """
  122. Get disk usage
  123. """
  124. try:
  125. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  126. data = pipe.read().strip().split('\n')
  127. pipe.close()
  128. data = [i.split(None, 6) for i in data]
  129. except Exception,err:
  130. data = str(err)
  131. return data
  132. def get_mem():
  133. """
  134. Get memory usage
  135. """
  136. try:
  137. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  138. data = pipe.read().strip().split()
  139. pipe.close()
  140. allmem = int(data[0])
  141. freemem = int(data[1])
  142. percent = (100 - ((freemem * 100) / allmem))
  143. usage = (allmem - freemem)
  144. mem_usage = {'usage': usage, 'percent': percent}
  145. data = mem_usage
  146. except Exception,err:
  147. data = str(err)
  148. return data
  149. def get_cpu_usage():
  150. """
  151. Get the CPU usage and running processes
  152. """
  153. try:
  154. pipe = os.popen("ps aux --sort -%cpu,-rss")
  155. data = pipe.read().strip().split('\n')
  156. pipe.close()
  157. usage = [i.split(None, 10) for i in data]
  158. del usage[0]
  159. total_usage = []
  160. for element in usage:
  161. usage_cpu = element[2]
  162. total_usage.append(usage_cpu)
  163. #del total_usage[0]
  164. total_usage = sum(float(i) for i in total_usage)
  165. total_free = (100 - float(total_usage))
  166. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  167. data = cpu_used
  168. except Exception,err:
  169. data = str(err)
  170. return data
  171. def get_load():
  172. """
  173. Get load average
  174. """
  175. try:
  176. data = os.getloadavg()[0]
  177. except Exception, err:
  178. data = str(err)
  179. return data
  180. def getall(request):
  181. if not request.user.is_authenticated():
  182. return HttpResponseRedirect('/login')
  183. return render_to_response('main.html', {'gethostname': get_hostname(),
  184. 'getplatform': get_platform(),
  185. 'getcpus': get_cpus(),
  186. 'time_refresh': time_refresh,
  187. 'time_refresh_long': time_refresh_long,
  188. 'time_refresh_net': time_refresh_net,
  189. 'version': version
  190. }, context_instance=RequestContext(request))