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.

327 lines
7.8 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
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. #The MIT License (MIT)
  2. #
  3. #Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/
  4. #
  5. #Permission is hereby granted, free of charge, to any person obtaining a copy
  6. #of this software and associated documentation files (the "Software"), to deal
  7. #in the Software without restriction, including without limitation the rights
  8. #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. #copies of the Software, and to permit persons to whom the Software is
  10. #furnished to do so, subject to the following conditions:
  11. #
  12. #The above copyright notice and this permission notice shall be included in all
  13. #copies or substantial portions of the Software.
  14. #
  15. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. #SOFTWARE.
  22. import platform, os, multiprocessing, json
  23. from datetime import timedelta
  24. from django.contrib.auth.decorators import login_required
  25. from django.core import serializers
  26. from django.shortcuts import render_to_response
  27. from django.http import HttpResponseRedirect, HttpResponse
  28. from django.template import RequestContext
  29. from django.utils.translation import ugettext_lazy as _
  30. from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION
  31. time_refresh = TIME_JS_REFRESH
  32. time_refresh_long = TIME_JS_REFRESH_LONG
  33. time_refresh_net = TIME_JS_REFRESH_NET
  34. version = VERSION
  35. @login_required(login_url='/login/')
  36. def index(request):
  37. """
  38. Index page.
  39. """
  40. return HttpResponseRedirect('/main')
  41. def chunks(get, n):
  42. return [get[i:i+n] for i in range(0, len(get), n)]
  43. def get_uptime():
  44. """
  45. Get uptime
  46. """
  47. try:
  48. with open('/proc/uptime', 'r') as f:
  49. uptime_seconds = float(f.readline().split()[0])
  50. uptime_time = str(timedelta(seconds = uptime_seconds))
  51. data = uptime_time.split('.', 1)[0]
  52. except Exception,err:
  53. data = str(err)
  54. return data
  55. def get_ipaddress():
  56. """
  57. Get the IP Address
  58. """
  59. try:
  60. if get_platform()['osname'] == "Linux":
  61. pipe = os.popen("ip addr | grep -A3 'LOWER_UP' | awk '{if ($2 == \"forever\"){printf \"unavailable,\"} else{ printf \"%s,\",$2}}'|awk -F,, '{print $0}'")
  62. else:
  63. pipe = os.popen("ip addr | grep -A3 'LOWER_UP' | awk '{if ($2 == \"forever\"){printf \"unavailable,,\"} else{ printf \"%s,\",$2}}'|awk -F,, '{print $0}'")
  64. data = pipe.read().strip().split(',,')
  65. pipe.close()
  66. data = [i.split(',', 4) for i in data]
  67. if len(data) == 2:
  68. del data[0]
  69. if len(data) > 2:
  70. data = data[1:-1]
  71. itf = []
  72. for e in data:
  73. itf.append(e[0].strip(':'))
  74. ips = {'interface': itf, 'itfip': data}
  75. data = ips
  76. except Exception,err:
  77. data = str(err)
  78. return data
  79. def get_cpus():
  80. """
  81. Get the number of CPUs and model/type
  82. """
  83. try:
  84. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'")
  85. data = pipe.read().strip().split(':')[-1]
  86. pipe.close()
  87. if not data:
  88. pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'")
  89. data = pipe.read().strip().split(':')[-1]
  90. pipe.close()
  91. cpus = multiprocessing.cpu_count()
  92. data = {'cpus': cpus, 'type': data}
  93. except Exception, err:
  94. data = str(err)
  95. return data
  96. def get_users():
  97. """
  98. Get the current logged in users
  99. """
  100. try:
  101. pipe = os.popen("who |" + "awk '{print $1, $2, $6}'")
  102. data = pipe.read().strip().split('\n')
  103. pipe.close()
  104. if data == [""]:
  105. data = None
  106. else:
  107. data = [i.split(None, 3) for i in data]
  108. except Exception, err:
  109. data = str(err)
  110. return data
  111. def get_traffic(request):
  112. """
  113. Get the traffic for the specified interface
  114. """
  115. try:
  116. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
  117. data = pipe.read().strip().split(':',1)[-1]
  118. pipe.close()
  119. if not data[0].isdigit():
  120. pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
  121. data = pipe.read().strip().split(':',1)[-1]
  122. pipe.close()
  123. data = data.split()
  124. traffic_in = int(data[0])
  125. traffic_out = int(data[1])
  126. all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
  127. data = all_traffic
  128. except Exception,err:
  129. data = str(err)
  130. return data
  131. def get_platform():
  132. """
  133. Get the OS name, hostname and kernel
  134. """
  135. try:
  136. osname = " ".join(platform.linux_distribution())
  137. uname = platform.uname()
  138. if osname == ' ':
  139. osname = uname[0]
  140. data = {'osname': osname, 'hostname': uname[1], 'kernel': uname[2] }
  141. except Exception,err:
  142. data = str(err)
  143. return data
  144. def get_disk():
  145. """
  146. Get disk usage
  147. """
  148. try:
  149. pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'")
  150. data = pipe.read().strip().split('\n')
  151. pipe.close()
  152. data = [i.split(None, 6) for i in data]
  153. except Exception,err:
  154. data = str(err)
  155. return data
  156. def get_disk_rw():
  157. """
  158. Get the disk reads and writes
  159. """
  160. try:
  161. pipe = os.popen("cat /proc/partitions | grep -v 'major' | awk '{print $4}'")
  162. data = pipe.read().strip().split('\n')
  163. pipe.close()
  164. rws = []
  165. for i in data:
  166. if i.isalpha():
  167. pipe = os.popen("cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'")
  168. rw = pipe.read().strip().split()
  169. pipe.close()
  170. rws.append([i, rw[0], rw[1]])
  171. if not rws:
  172. pipe = os.popen("cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'")
  173. rw = pipe.read().strip().split()
  174. pipe.close()
  175. rws.append([data[0], rw[0], rw[1]])
  176. data = rws
  177. except Exception,err:
  178. data = str(err)
  179. return data
  180. def get_mem():
  181. """
  182. Get memory usage
  183. """
  184. try:
  185. pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'")
  186. data = pipe.read().strip().split()
  187. pipe.close()
  188. allmem = int(data[0])
  189. freemem = int(data[1])
  190. percent = (100 - ((freemem * 100) / allmem))
  191. usage = (allmem - freemem)
  192. mem_usage = {'usage': usage, 'percent': percent}
  193. data = mem_usage
  194. except Exception,err:
  195. data = str(err)
  196. return data
  197. def get_cpu_usage():
  198. """
  199. Get the CPU usage and running processes
  200. """
  201. try:
  202. pipe = os.popen("ps aux --sort -%cpu,-rss")
  203. data = pipe.read().strip().split('\n')
  204. pipe.close()
  205. usage = [i.split(None, 10) for i in data]
  206. del usage[0]
  207. total_usage = []
  208. for element in usage:
  209. usage_cpu = element[2]
  210. total_usage.append(usage_cpu)
  211. total_usage = sum(float(i) for i in total_usage)
  212. total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
  213. cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage}
  214. data = cpu_used
  215. except Exception,err:
  216. data = str(err)
  217. return data
  218. def get_load():
  219. """
  220. Get load average
  221. """
  222. try:
  223. data = os.getloadavg()[0]
  224. except Exception, err:
  225. data = str(err)
  226. return data
  227. def get_netstat():
  228. """
  229. Get ports and applications
  230. """
  231. try:
  232. pipe = os.popen("ss -tnp | grep ESTAB | awk '{print $4, $5}'| sed 's/::ffff://g' | awk -F: '{print $1, $2}' | sort -n | uniq -c")
  233. data = pipe.read().strip().split('\n')
  234. pipe.close()
  235. data = [i.split(None, 4) for i in data]
  236. except Exception, err:
  237. data = str(err)
  238. return data
  239. @login_required(login_url='/login/')
  240. def getall(request):
  241. return render_to_response('main.html', {'time_refresh': time_refresh,
  242. 'time_refresh_long': time_refresh_long,
  243. 'time_refresh_net': time_refresh_net,
  244. 'version': version
  245. }, context_instance=RequestContext(request))