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