monitordisk.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. #
  5. # Copyright (C) 2012 Robert Yang
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. import os, logging, re, sys
  20. import bb
  21. logger = logging.getLogger("BitBake.Monitor")
  22. def printErr(info):
  23. logger.error("%s\n Disk space monitor will NOT be enabled" % info)
  24. def convertGMK(unit):
  25. """ Convert the space unit G, M, K, the unit is case-insensitive """
  26. unitG = re.match('([1-9][0-9]*)[gG]\s?$', unit)
  27. if unitG:
  28. return int(unitG.group(1)) * (1024 ** 3)
  29. unitM = re.match('([1-9][0-9]*)[mM]\s?$', unit)
  30. if unitM:
  31. return int(unitM.group(1)) * (1024 ** 2)
  32. unitK = re.match('([1-9][0-9]*)[kK]\s?$', unit)
  33. if unitK:
  34. return int(unitK.group(1)) * 1024
  35. unitN = re.match('([1-9][0-9]*)\s?$', unit)
  36. if unitN:
  37. return int(unitN.group(1))
  38. else:
  39. return None
  40. def getMountedDev(path):
  41. """ Get the device mounted at the path, uses /proc/mounts """
  42. # Get the mount point of the filesystem containing path
  43. # st_dev is the ID of device containing file
  44. parentDev = os.stat(path).st_dev
  45. currentDev = parentDev
  46. # When the current directory's device is different from the
  47. # parent's, then the current directory is a mount point
  48. while parentDev == currentDev:
  49. mountPoint = path
  50. # Use dirname to get the parent's directory
  51. path = os.path.dirname(path)
  52. # Reach the "/"
  53. if path == mountPoint:
  54. break
  55. parentDev= os.stat(path).st_dev
  56. try:
  57. with open("/proc/mounts", "r") as ifp:
  58. for line in ifp:
  59. procLines = line.rstrip('\n').split()
  60. if procLines[1] == mountPoint:
  61. return procLines[0]
  62. except EnvironmentError:
  63. pass
  64. return None
  65. def getDiskData(BBDirs, configuration):
  66. """Prepare disk data for disk space monitor"""
  67. # Save the device IDs, need the ID to be unique (the dictionary's key is
  68. # unique), so that when more than one directory is located on the same
  69. # device, we just monitor it once
  70. devDict = {}
  71. for pathSpaceInode in BBDirs.split():
  72. # The input format is: "dir,space,inode", dir is a must, space
  73. # and inode are optional
  74. pathSpaceInodeRe = re.match('([^,]*),([^,]*),([^,]*),?(.*)', pathSpaceInode)
  75. if not pathSpaceInodeRe:
  76. printErr("Invalid value in BB_DISKMON_DIRS: %s" % pathSpaceInode)
  77. return None
  78. action = pathSpaceInodeRe.group(1)
  79. if action not in ("ABORT", "STOPTASKS", "WARN"):
  80. printErr("Unknown disk space monitor action: %s" % action)
  81. return None
  82. path = os.path.realpath(pathSpaceInodeRe.group(2))
  83. if not path:
  84. printErr("Invalid path value in BB_DISKMON_DIRS: %s" % pathSpaceInode)
  85. return None
  86. # The disk space or inode is optional, but it should have a correct
  87. # value once it is specified
  88. minSpace = pathSpaceInodeRe.group(3)
  89. if minSpace:
  90. minSpace = convertGMK(minSpace)
  91. if not minSpace:
  92. printErr("Invalid disk space value in BB_DISKMON_DIRS: %s" % pathSpaceInodeRe.group(3))
  93. return None
  94. else:
  95. # None means that it is not specified
  96. minSpace = None
  97. minInode = pathSpaceInodeRe.group(4)
  98. if minInode:
  99. minInode = convertGMK(minInode)
  100. if not minInode:
  101. printErr("Invalid inode value in BB_DISKMON_DIRS: %s" % pathSpaceInodeRe.group(4))
  102. return None
  103. else:
  104. # None means that it is not specified
  105. minInode = None
  106. if minSpace is None and minInode is None:
  107. printErr("No disk space or inode value in found BB_DISKMON_DIRS: %s" % pathSpaceInode)
  108. return None
  109. # mkdir for the directory since it may not exist, for example the
  110. # DL_DIR may not exist at the very beginning
  111. if not os.path.exists(path):
  112. bb.utils.mkdirhier(path)
  113. dev = getMountedDev(path)
  114. # Use path/action as the key
  115. devDict[os.path.join(path, action)] = [dev, minSpace, minInode]
  116. return devDict
  117. def getInterval(configuration):
  118. """ Get the disk space interval """
  119. # The default value is 50M and 5K.
  120. spaceDefault = 50 * 1024 * 1024
  121. inodeDefault = 5 * 1024
  122. interval = configuration.getVar("BB_DISKMON_WARNINTERVAL", True)
  123. if not interval:
  124. return spaceDefault, inodeDefault
  125. else:
  126. # The disk space or inode interval is optional, but it should
  127. # have a correct value once it is specified
  128. intervalRe = re.match('([^,]*),?\s*(.*)', interval)
  129. if intervalRe:
  130. intervalSpace = intervalRe.group(1)
  131. if intervalSpace:
  132. intervalSpace = convertGMK(intervalSpace)
  133. if not intervalSpace:
  134. printErr("Invalid disk space interval value in BB_DISKMON_WARNINTERVAL: %s" % intervalRe.group(1))
  135. return None, None
  136. else:
  137. intervalSpace = spaceDefault
  138. intervalInode = intervalRe.group(2)
  139. if intervalInode:
  140. intervalInode = convertGMK(intervalInode)
  141. if not intervalInode:
  142. printErr("Invalid disk inode interval value in BB_DISKMON_WARNINTERVAL: %s" % intervalRe.group(2))
  143. return None, None
  144. else:
  145. intervalInode = inodeDefault
  146. return intervalSpace, intervalInode
  147. else:
  148. printErr("Invalid interval value in BB_DISKMON_WARNINTERVAL: %s" % interval)
  149. return None, None
  150. class diskMonitor:
  151. """Prepare the disk space monitor data"""
  152. def __init__(self, configuration):
  153. self.enableMonitor = False
  154. self.configuration = configuration
  155. BBDirs = configuration.getVar("BB_DISKMON_DIRS", True) or None
  156. if BBDirs:
  157. self.devDict = getDiskData(BBDirs, configuration)
  158. if self.devDict:
  159. self.spaceInterval, self.inodeInterval = getInterval(configuration)
  160. if self.spaceInterval and self.inodeInterval:
  161. self.enableMonitor = True
  162. # These are for saving the previous disk free space and inode, we
  163. # use them to avoid printing too many warning messages
  164. self.preFreeS = {}
  165. self.preFreeI = {}
  166. # This is for STOPTASKS and ABORT, to avoid printing the message
  167. # repeatedly while waiting for the tasks to finish
  168. self.checked = {}
  169. for k in self.devDict:
  170. self.preFreeS[k] = 0
  171. self.preFreeI[k] = 0
  172. self.checked[k] = False
  173. if self.spaceInterval is None and self.inodeInterval is None:
  174. self.enableMonitor = False
  175. def check(self, rq):
  176. """ Take action for the monitor """
  177. if self.enableMonitor:
  178. for k in self.devDict:
  179. path = os.path.dirname(k)
  180. action = os.path.basename(k)
  181. dev = self.devDict[k][0]
  182. minSpace = self.devDict[k][1]
  183. minInode = self.devDict[k][2]
  184. st = os.statvfs(path)
  185. # The free space, float point number
  186. freeSpace = st.f_bavail * st.f_frsize
  187. if minSpace and freeSpace < minSpace:
  188. # Always show warning, the self.checked would always be False if the action is WARN
  189. if self.preFreeS[k] == 0 or self.preFreeS[k] - freeSpace > self.spaceInterval and not self.checked[k]:
  190. logger.warning("The free space of %s (%s) is running low (%.3fGB left)" % \
  191. (path, dev, freeSpace / 1024 / 1024 / 1024.0))
  192. self.preFreeS[k] = freeSpace
  193. if action == "STOPTASKS" and not self.checked[k]:
  194. logger.error("No new tasks can be executed since the disk space monitor action is \"STOPTASKS\"!")
  195. self.checked[k] = True
  196. rq.finish_runqueue(False)
  197. bb.event.fire(bb.event.DiskFull(dev, 'disk', freeSpace, path), self.configuration)
  198. elif action == "ABORT" and not self.checked[k]:
  199. logger.error("Immediately abort since the disk space monitor action is \"ABORT\"!")
  200. self.checked[k] = True
  201. rq.finish_runqueue(True)
  202. bb.event.fire(bb.event.DiskFull(dev, 'disk', freeSpace, path), self.configuration)
  203. # The free inodes, float point number
  204. freeInode = st.f_favail
  205. if minInode and freeInode < minInode:
  206. # Some filesystems use dynamic inodes so can't run out
  207. # (e.g. btrfs). This is reported by the inode count being 0.
  208. if st.f_files == 0:
  209. self.devDict[k][2] = None
  210. continue
  211. # Always show warning, the self.checked would always be False if the action is WARN
  212. if self.preFreeI[k] == 0 or self.preFreeI[k] - freeInode > self.inodeInterval and not self.checked[k]:
  213. logger.warning("The free inode of %s (%s) is running low (%.3fK left)" % \
  214. (path, dev, freeInode / 1024.0))
  215. self.preFreeI[k] = freeInode
  216. if action == "STOPTASKS" and not self.checked[k]:
  217. logger.error("No new tasks can be executed since the disk space monitor action is \"STOPTASKS\"!")
  218. self.checked[k] = True
  219. rq.finish_runqueue(False)
  220. bb.event.fire(bb.event.DiskFull(dev, 'inode', freeInode, path), self.configuration)
  221. elif action == "ABORT" and not self.checked[k]:
  222. logger.error("Immediately abort since the disk space monitor action is \"ABORT\"!")
  223. self.checked[k] = True
  224. rq.finish_runqueue(True)
  225. bb.event.fire(bb.event.DiskFull(dev, 'inode', freeInode, path), self.configuration)
  226. return