kanrisuru 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (252) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/kanrisuru/core/apt/commands/autoclean.rb +13 -0
  4. data/lib/kanrisuru/core/apt/commands/autoremove.rb +15 -0
  5. data/lib/kanrisuru/core/apt/commands/clean.rb +13 -0
  6. data/lib/kanrisuru/core/apt/commands/full_upgrade.rb +14 -0
  7. data/lib/kanrisuru/core/apt/commands/install.rb +22 -0
  8. data/lib/kanrisuru/core/apt/commands/list.rb +21 -0
  9. data/lib/kanrisuru/core/apt/commands/purge.rb +18 -0
  10. data/lib/kanrisuru/core/apt/commands/remove.rb +18 -0
  11. data/lib/kanrisuru/core/apt/commands/search.rb +18 -0
  12. data/lib/kanrisuru/core/apt/commands/show.rb +21 -0
  13. data/lib/kanrisuru/core/apt/commands/update.rb +14 -0
  14. data/lib/kanrisuru/core/apt/commands/upgrade.rb +14 -0
  15. data/lib/kanrisuru/core/apt/commands.rb +14 -0
  16. data/lib/kanrisuru/core/apt/parser.rb +6 -0
  17. data/lib/kanrisuru/core/apt/parsers/base.rb +51 -0
  18. data/lib/kanrisuru/core/apt/parsers/list.rb +28 -0
  19. data/lib/kanrisuru/core/apt/parsers/search.rb +32 -0
  20. data/lib/kanrisuru/core/apt/parsers/show.rb +90 -0
  21. data/lib/kanrisuru/core/apt/types.rb +36 -0
  22. data/lib/kanrisuru/core/apt.rb +4 -284
  23. data/lib/kanrisuru/core/archive/commands/tar.rb +126 -0
  24. data/lib/kanrisuru/core/archive/commands.rb +3 -0
  25. data/lib/kanrisuru/core/archive/types.rb +9 -0
  26. data/lib/kanrisuru/core/archive.rb +3 -121
  27. data/lib/kanrisuru/core/disk/commands/blkid.rb +35 -0
  28. data/lib/kanrisuru/core/disk/commands/df.rb +26 -0
  29. data/lib/kanrisuru/core/disk/commands/du.rb +24 -0
  30. data/lib/kanrisuru/core/disk/commands/lsblk.rb +54 -0
  31. data/lib/kanrisuru/core/disk/commands.rb +6 -0
  32. data/lib/kanrisuru/core/disk/parser.rb +6 -0
  33. data/lib/kanrisuru/core/disk/parsers/blkid.rb +72 -0
  34. data/lib/kanrisuru/core/disk/parsers/df.rb +33 -0
  35. data/lib/kanrisuru/core/disk/parsers/du.rb +25 -0
  36. data/lib/kanrisuru/core/disk/parsers/lsblk.rb +96 -0
  37. data/lib/kanrisuru/core/disk/types.rb +21 -0
  38. data/lib/kanrisuru/core/disk.rb +5 -290
  39. data/lib/kanrisuru/core/dmi/commands/dmi.rb +42 -0
  40. data/lib/kanrisuru/core/dmi/commands.rb +3 -0
  41. data/lib/kanrisuru/core/dmi/parser.rb +3 -0
  42. data/lib/kanrisuru/core/dmi/parsers/dmi.rb +149 -0
  43. data/lib/kanrisuru/core/dmi/types.rb +361 -0
  44. data/lib/kanrisuru/core/dmi.rb +4 -522
  45. data/lib/kanrisuru/core/file/commands/chmod.rb +29 -0
  46. data/lib/kanrisuru/core/file/commands/chown.rb +35 -0
  47. data/lib/kanrisuru/core/file/commands/copy.rb +56 -0
  48. data/lib/kanrisuru/core/file/commands/link.rb +25 -0
  49. data/lib/kanrisuru/core/file/commands/mkdir.rb +37 -0
  50. data/lib/kanrisuru/core/file/commands/move.rb +48 -0
  51. data/lib/kanrisuru/core/file/commands/rm.rb +38 -0
  52. data/lib/kanrisuru/core/file/commands/symlink.rb +46 -0
  53. data/lib/kanrisuru/core/file/commands/touch.rb +33 -0
  54. data/lib/kanrisuru/core/file/commands/unlink.rb +13 -0
  55. data/lib/kanrisuru/core/file/commands/wc.rb +16 -0
  56. data/lib/kanrisuru/core/file/commands.rb +29 -0
  57. data/lib/kanrisuru/core/file/parser.rb +3 -0
  58. data/lib/kanrisuru/core/file/parsers/wc.rb +16 -0
  59. data/lib/kanrisuru/core/file/types.rb +9 -0
  60. data/lib/kanrisuru/core/file.rb +4 -312
  61. data/lib/kanrisuru/core/find/commands/find.rb +97 -0
  62. data/lib/kanrisuru/core/find/commands.rb +3 -0
  63. data/lib/kanrisuru/core/find/constants.rb +9 -0
  64. data/lib/kanrisuru/core/find/parser.rb +3 -0
  65. data/lib/kanrisuru/core/find/parsers/find.rb +18 -0
  66. data/lib/kanrisuru/core/find/types.rb +9 -0
  67. data/lib/kanrisuru/core/find.rb +5 -97
  68. data/lib/kanrisuru/core/group/commands/create_group.rb +21 -0
  69. data/lib/kanrisuru/core/group/commands/delete_group.rb +16 -0
  70. data/lib/kanrisuru/core/group/commands/get_gid.rb +17 -0
  71. data/lib/kanrisuru/core/group/commands/get_group.rb +29 -0
  72. data/lib/kanrisuru/core/group/commands/is_group.rb +14 -0
  73. data/lib/kanrisuru/core/group/commands/update_group.rb +23 -0
  74. data/lib/kanrisuru/core/group/commands.rb +8 -0
  75. data/lib/kanrisuru/core/group/parser.rb +4 -0
  76. data/lib/kanrisuru/core/group/parsers/gid.rb +15 -0
  77. data/lib/kanrisuru/core/group/parsers/group.rb +15 -0
  78. data/lib/kanrisuru/core/group/types.rb +10 -0
  79. data/lib/kanrisuru/core/group.rb +4 -81
  80. data/lib/kanrisuru/core/ip/commands/address.rb +69 -0
  81. data/lib/kanrisuru/core/ip/commands/address_label.rb +38 -0
  82. data/lib/kanrisuru/core/ip/commands/link.rb +74 -0
  83. data/lib/kanrisuru/core/ip/commands/maddress.rb +33 -0
  84. data/lib/kanrisuru/core/ip/commands/neighbour.rb +55 -0
  85. data/lib/kanrisuru/core/ip/commands/route.rb +115 -0
  86. data/lib/kanrisuru/core/ip/commands/rule.rb +42 -0
  87. data/lib/kanrisuru/core/ip/commands.rb +24 -0
  88. data/lib/kanrisuru/core/ip/constants.rb +13 -0
  89. data/lib/kanrisuru/core/ip/parser.rb +10 -0
  90. data/lib/kanrisuru/core/ip/parsers/address.rb +104 -0
  91. data/lib/kanrisuru/core/ip/parsers/address_label.rb +27 -0
  92. data/lib/kanrisuru/core/ip/parsers/base.rb +166 -0
  93. data/lib/kanrisuru/core/ip/parsers/link.rb +86 -0
  94. data/lib/kanrisuru/core/ip/parsers/maddress.rb +85 -0
  95. data/lib/kanrisuru/core/ip/parsers/neighbour.rb +89 -0
  96. data/lib/kanrisuru/core/ip/parsers/route.rb +76 -0
  97. data/lib/kanrisuru/core/ip/parsers/rule.rb +60 -0
  98. data/lib/kanrisuru/core/ip/types.rb +46 -0
  99. data/lib/kanrisuru/core/ip.rb +5 -995
  100. data/lib/kanrisuru/core/mount/commands/mount.rb +65 -0
  101. data/lib/kanrisuru/core/mount/commands/umount.rb +35 -0
  102. data/lib/kanrisuru/core/mount/commands.rb +49 -0
  103. data/lib/kanrisuru/core/mount.rb +2 -125
  104. data/lib/kanrisuru/core/path/commands/ls.rb +26 -0
  105. data/lib/kanrisuru/core/path/commands/pwd.rb +16 -0
  106. data/lib/kanrisuru/core/path/commands/readlink.rb +22 -0
  107. data/lib/kanrisuru/core/path/commands/realpath.rb +18 -0
  108. data/lib/kanrisuru/core/path/commands/which.rb +19 -0
  109. data/lib/kanrisuru/core/path/commands/whoami.rb +16 -0
  110. data/lib/kanrisuru/core/path/commands.rb +8 -0
  111. data/lib/kanrisuru/core/path/parser.rb +4 -0
  112. data/lib/kanrisuru/core/path/parsers/ls.rb +53 -0
  113. data/lib/kanrisuru/core/path/parsers/which.rb +18 -0
  114. data/lib/kanrisuru/core/path/types.rb +12 -0
  115. data/lib/kanrisuru/core/path.rb +4 -121
  116. data/lib/kanrisuru/core/socket/commands/ss.rb +44 -0
  117. data/lib/kanrisuru/core/socket/commands.rb +3 -0
  118. data/lib/kanrisuru/core/socket/constants.rb +29 -0
  119. data/lib/kanrisuru/core/socket/parser.rb +3 -0
  120. data/lib/kanrisuru/core/socket/parsers/ss.rb +109 -0
  121. data/lib/kanrisuru/core/socket/types.rb +18 -0
  122. data/lib/kanrisuru/core/socket.rb +5 -164
  123. data/lib/kanrisuru/core/stat/commands/stat.rb +62 -0
  124. data/lib/kanrisuru/core/stat/commands.rb +3 -0
  125. data/lib/kanrisuru/core/stat/parser.rb +3 -0
  126. data/lib/kanrisuru/core/stat/parsers/stat.rb +34 -0
  127. data/lib/kanrisuru/core/stat/types.rb +14 -0
  128. data/lib/kanrisuru/core/stat.rb +4 -81
  129. data/lib/kanrisuru/core/stream/commands/cat.rb +25 -0
  130. data/lib/kanrisuru/core/stream/commands/echo.rb +20 -0
  131. data/lib/kanrisuru/core/stream/commands/head.rb +18 -0
  132. data/lib/kanrisuru/core/stream/commands/read_file_chunk.rb +21 -0
  133. data/lib/kanrisuru/core/stream/commands/sed.rb +23 -0
  134. data/lib/kanrisuru/core/stream/commands/tail.rb +18 -0
  135. data/lib/kanrisuru/core/stream/commands.rb +27 -0
  136. data/lib/kanrisuru/core/stream/parser.rb +4 -0
  137. data/lib/kanrisuru/core/stream/parsers/echo.rb +17 -0
  138. data/lib/kanrisuru/core/stream/parsers/sed.rb +17 -0
  139. data/lib/kanrisuru/core/stream.rb +3 -104
  140. data/lib/kanrisuru/core/system/commands/cpu_info.rb +32 -0
  141. data/lib/kanrisuru/core/system/commands/free.rb +25 -0
  142. data/lib/kanrisuru/core/system/commands/kernel_statistics.rb +21 -0
  143. data/lib/kanrisuru/core/system/commands/kill.rb +22 -0
  144. data/lib/kanrisuru/core/system/commands/last.rb +31 -0
  145. data/lib/kanrisuru/core/system/commands/load_average.rb +16 -0
  146. data/lib/kanrisuru/core/system/commands/load_env.rb +16 -0
  147. data/lib/kanrisuru/core/system/commands/lscpu.rb +17 -0
  148. data/lib/kanrisuru/core/system/commands/lsof.rb +16 -0
  149. data/lib/kanrisuru/core/system/commands/poweroff.rb +42 -0
  150. data/lib/kanrisuru/core/system/commands/ps.rb +35 -0
  151. data/lib/kanrisuru/core/system/commands/reboot.rb +42 -0
  152. data/lib/kanrisuru/core/system/commands/uptime.rb +18 -0
  153. data/lib/kanrisuru/core/system/commands/w.rb +24 -0
  154. data/lib/kanrisuru/core/system/commands.rb +34 -0
  155. data/lib/kanrisuru/core/system/parser.rb +11 -0
  156. data/lib/kanrisuru/core/system/parsers/kernel_statistics.rb +56 -0
  157. data/lib/kanrisuru/core/system/parsers/last.rb +64 -0
  158. data/lib/kanrisuru/core/system/parsers/load_average.rb +15 -0
  159. data/lib/kanrisuru/core/system/parsers/load_env.rb +24 -0
  160. data/lib/kanrisuru/core/system/parsers/lscpu.rb +90 -0
  161. data/lib/kanrisuru/core/system/parsers/lsof.rb +62 -0
  162. data/lib/kanrisuru/core/system/parsers/ps.rb +71 -0
  163. data/lib/kanrisuru/core/system/parsers/uptime.rb +38 -0
  164. data/lib/kanrisuru/core/system/parsers/w.rb +30 -0
  165. data/lib/kanrisuru/core/system/types.rb +108 -0
  166. data/lib/kanrisuru/core/system.rb +4 -687
  167. data/lib/kanrisuru/core/transfer/commands/download.rb +26 -0
  168. data/lib/kanrisuru/core/transfer/commands/upload.rb +23 -0
  169. data/lib/kanrisuru/core/transfer/commands/wget.rb +168 -0
  170. data/lib/kanrisuru/core/transfer/commands.rb +5 -0
  171. data/lib/kanrisuru/core/transfer/constants.rb +10 -0
  172. data/lib/kanrisuru/core/transfer.rb +3 -199
  173. data/lib/kanrisuru/core/user/commands/create_user.rb +57 -0
  174. data/lib/kanrisuru/core/user/commands/delete_user.rb +20 -0
  175. data/lib/kanrisuru/core/user/commands/get_uid.rb +15 -0
  176. data/lib/kanrisuru/core/user/commands/get_user.rb +48 -0
  177. data/lib/kanrisuru/core/user/commands/is_user.rb +14 -0
  178. data/lib/kanrisuru/core/user/commands/update_user.rb +63 -0
  179. data/lib/kanrisuru/core/user/commands.rb +8 -0
  180. data/lib/kanrisuru/core/user/parser.rb +4 -0
  181. data/lib/kanrisuru/core/user/parsers/getent.rb +17 -0
  182. data/lib/kanrisuru/core/user/parsers/groups.rb +17 -0
  183. data/lib/kanrisuru/core/user/types.rb +11 -0
  184. data/lib/kanrisuru/core/user.rb +4 -182
  185. data/lib/kanrisuru/core/yum/commands/autoremove.rb +14 -0
  186. data/lib/kanrisuru/core/yum/commands/clean.rb +23 -0
  187. data/lib/kanrisuru/core/yum/commands/erase.rb +20 -0
  188. data/lib/kanrisuru/core/yum/commands/info.rb +21 -0
  189. data/lib/kanrisuru/core/yum/commands/install.rb +18 -0
  190. data/lib/kanrisuru/core/yum/commands/list.rb +30 -0
  191. data/lib/kanrisuru/core/yum/commands/localinstall.rb +22 -0
  192. data/lib/kanrisuru/core/yum/commands/remove.rb +20 -0
  193. data/lib/kanrisuru/core/yum/commands/repolist.rb +20 -0
  194. data/lib/kanrisuru/core/yum/commands/search.rb +21 -0
  195. data/lib/kanrisuru/core/yum/commands/update.rb +14 -0
  196. data/lib/kanrisuru/core/yum/commands/upgrade.rb +14 -0
  197. data/lib/kanrisuru/core/yum/commands.rb +39 -0
  198. data/lib/kanrisuru/core/yum/parser.rb +7 -0
  199. data/lib/kanrisuru/core/yum/parsers/base.rb +31 -0
  200. data/lib/kanrisuru/core/yum/parsers/info.rb +69 -0
  201. data/lib/kanrisuru/core/yum/parsers/list.rb +24 -0
  202. data/lib/kanrisuru/core/yum/parsers/repolist.rb +60 -0
  203. data/lib/kanrisuru/core/yum/parsers/search.rb +30 -0
  204. data/lib/kanrisuru/core/yum/types.rb +41 -0
  205. data/lib/kanrisuru/core/yum.rb +4 -347
  206. data/lib/kanrisuru/core/zypper/commands/add_lock.rb +22 -0
  207. data/lib/kanrisuru/core/zypper/commands/add_repo.rb +38 -0
  208. data/lib/kanrisuru/core/zypper/commands/add_service.rb +26 -0
  209. data/lib/kanrisuru/core/zypper/commands/clean_cache.rb +22 -0
  210. data/lib/kanrisuru/core/zypper/commands/clean_locks.rb +17 -0
  211. data/lib/kanrisuru/core/zypper/commands/dist_upgrade.rb +25 -0
  212. data/lib/kanrisuru/core/zypper/commands/info.rb +25 -0
  213. data/lib/kanrisuru/core/zypper/commands/install.rb +39 -0
  214. data/lib/kanrisuru/core/zypper/commands/install_new_recommends.rb +23 -0
  215. data/lib/kanrisuru/core/zypper/commands/list_locks.rb +22 -0
  216. data/lib/kanrisuru/core/zypper/commands/list_patches.rb +34 -0
  217. data/lib/kanrisuru/core/zypper/commands/list_repos.rb +21 -0
  218. data/lib/kanrisuru/core/zypper/commands/list_services.rb +21 -0
  219. data/lib/kanrisuru/core/zypper/commands/list_updates.rb +30 -0
  220. data/lib/kanrisuru/core/zypper/commands/modify_repo.rb +48 -0
  221. data/lib/kanrisuru/core/zypper/commands/modify_service.rb +40 -0
  222. data/lib/kanrisuru/core/zypper/commands/patch.rb +40 -0
  223. data/lib/kanrisuru/core/zypper/commands/patch_check.rb +29 -0
  224. data/lib/kanrisuru/core/zypper/commands/purge_kernels.rb +18 -0
  225. data/lib/kanrisuru/core/zypper/commands/refresh_repos.rb +24 -0
  226. data/lib/kanrisuru/core/zypper/commands/refresh_services.rb +21 -0
  227. data/lib/kanrisuru/core/zypper/commands/remove.rb +26 -0
  228. data/lib/kanrisuru/core/zypper/commands/remove_lock.rb +22 -0
  229. data/lib/kanrisuru/core/zypper/commands/remove_repo.rb +32 -0
  230. data/lib/kanrisuru/core/zypper/commands/remove_service.rb +21 -0
  231. data/lib/kanrisuru/core/zypper/commands/rename_repo.rb +20 -0
  232. data/lib/kanrisuru/core/zypper/commands/search.rb +52 -0
  233. data/lib/kanrisuru/core/zypper/commands/source_install.rb +22 -0
  234. data/lib/kanrisuru/core/zypper/commands/update.rb +27 -0
  235. data/lib/kanrisuru/core/zypper/commands/verify.rb +23 -0
  236. data/lib/kanrisuru/core/zypper/commands.rb +109 -0
  237. data/lib/kanrisuru/core/zypper/constants.rb +19 -0
  238. data/lib/kanrisuru/core/zypper/parser.rb +11 -0
  239. data/lib/kanrisuru/core/zypper/parsers/base.rb +18 -0
  240. data/lib/kanrisuru/core/zypper/parsers/info.rb +77 -0
  241. data/lib/kanrisuru/core/zypper/parsers/list_locks.rb +36 -0
  242. data/lib/kanrisuru/core/zypper/parsers/list_patches.rb +40 -0
  243. data/lib/kanrisuru/core/zypper/parsers/list_repos.rb +37 -0
  244. data/lib/kanrisuru/core/zypper/parsers/list_services.rb +36 -0
  245. data/lib/kanrisuru/core/zypper/parsers/list_updates.rb +32 -0
  246. data/lib/kanrisuru/core/zypper/parsers/patch_check.rb +34 -0
  247. data/lib/kanrisuru/core/zypper/parsers/search.rb +37 -0
  248. data/lib/kanrisuru/core/zypper/types.rb +88 -0
  249. data/lib/kanrisuru/core/zypper.rb +5 -998
  250. data/lib/kanrisuru/version.rb +1 -1
  251. data/spec/unit/util_spec.rb +16 -0
  252. metadata +231 -2
@@ -3,6 +3,10 @@
3
3
  require 'date'
4
4
  require 'ipaddr'
5
5
 
6
+ require_relative 'system/types'
7
+ require_relative 'system/parser'
8
+ require_relative 'system/commands'
9
+
6
10
  module Kanrisuru
7
11
  module Core
8
12
  module System
@@ -15,702 +19,15 @@ module Kanrisuru
15
19
  os_define :linux, :free
16
20
  os_define :linux, :ps
17
21
  os_define :linux, :kill
18
-
19
22
  os_define :linux, :kernel_statistics
20
23
  os_define :linux, :kstat
21
-
22
24
  os_define :linux, :lsof
23
25
  os_define :linux, :last
24
-
25
26
  os_define :linux, :uptime
26
-
27
27
  os_define :linux, :w
28
28
  os_define :linux, :who
29
-
30
29
  os_define :linux, :reboot
31
30
  os_define :linux, :poweroff
32
-
33
- CPUArchitectureVulnerability = Struct.new(
34
- :name,
35
- :data
36
- )
37
-
38
- CPUArchitecture = Struct.new(
39
- :architecture,
40
- :cores,
41
- :byte_order,
42
- :address_sizes,
43
- :operation_modes,
44
- :online_cpus,
45
- :threads_per_core,
46
- :cores_per_socket,
47
- :sockets,
48
- :numa_mode,
49
- :vendor_id,
50
- :cpu_family,
51
- :model,
52
- :model_name,
53
- :stepping,
54
- :cpu_mhz,
55
- :cpu_max_mhz,
56
- :cpu_min_mhz,
57
- :bogo_mips,
58
- :virtualization,
59
- :hypervisor_vendor,
60
- :virtualization_type,
61
- :l1d_cache,
62
- :l1i_cache,
63
- :l2_cache,
64
- :l3_cache,
65
- :numa_nodes,
66
- :vulnerabilities,
67
- :flags
68
- )
69
-
70
- KernelStatisticCpu = Struct.new(
71
- :user,
72
- :nice,
73
- :system,
74
- :idle,
75
- :iowait,
76
- :irq,
77
- :softirq,
78
- :steal,
79
- :guest,
80
- :guest_nice
81
- )
82
-
83
- KernelStatistic = Struct.new(
84
- :cpu_total,
85
- :cpus,
86
- :interrupt_total,
87
- :interrupts,
88
- :ctxt,
89
- :btime,
90
- :processes,
91
- :procs_running,
92
- :procs_blocked,
93
- :softirq_total,
94
- :softirqs
95
- )
96
-
97
- ProcessInfo = Struct.new(
98
- :uid, :user, :gid, :group, :ppid, :pid,
99
- :cpu_usage, :memory_usage, :stat, :priority,
100
- :flags, :policy_abbr, :policy, :cpu_time, :command
101
- )
102
-
103
- Uptime = Struct.new(
104
- :boot_time, :uptime, :seconds, :minutes, :hours, :days
105
- )
106
-
107
- UserLoggedIn = Struct.new(:user, :tty, :ip, :login, :idle, :jcpu, :pcpu, :command)
108
-
109
- OpenFile = Struct.new(
110
- :command,
111
- :pid,
112
- :uid,
113
- :file_descriptor,
114
- :type,
115
- :device,
116
- :fsize,
117
- :inode,
118
- :name
119
- )
120
-
121
- SessionDetail = Struct.new(
122
- :tty,
123
- :login_at,
124
- :logout_at,
125
- :ip_address,
126
- :success
127
- )
128
-
129
- LoginUser = Struct.new(
130
- :user,
131
- :sessions
132
- )
133
-
134
- def load_env
135
- command = Kanrisuru::Command.new('env')
136
- execute_shell(command)
137
-
138
- Kanrisuru::Result.new(command) do |cmd|
139
- string = cmd.to_s
140
- hash = {}
141
-
142
- rows = string.split("\n")
143
- rows.each do |row|
144
- key, value = row.split('=', 2)
145
- hash[key] = value
146
- end
147
-
148
- hash
149
- end
150
- end
151
-
152
- def lscpu
153
- command = Kanrisuru::Command.new('lscpu')
154
-
155
- execute_shell(command)
156
-
157
- Kanrisuru::Result.new(command) do |cmd|
158
- lines = cmd.to_a
159
-
160
- result = CPUArchitecture.new
161
- result.vulnerabilities = []
162
- result.numa_nodes = []
163
-
164
- lines.each do |line|
165
- values = line.split(': ', 2)
166
-
167
- field = values[0].strip
168
- data = values[1].strip
169
-
170
- case field
171
- when 'Architecture'
172
- result.architecture = data
173
- when 'CPU op-mode(s)'
174
- result.operation_modes = data.split(', ')
175
- when 'Byte Order'
176
- result.byte_order = data
177
- when 'Address sizes'
178
- result.address_sizes = data.split(', ')
179
- when 'CPU(s)'
180
- result.cores = data.to_i
181
- when 'On-line CPU(s) list'
182
- result.online_cpus = data.to_i
183
- when 'Thread(s) per core'
184
- result.threads_per_core = data.to_i
185
- when 'Core(s) per socket'
186
- result.cores_per_socket = data.to_i
187
- when 'Socket(s)'
188
- result.sockets = data.to_i
189
- when 'NUMA node(s)'
190
- result.numa_nodes = data.to_i
191
- when 'Vendor ID'
192
- result.vendor_id = data
193
- when 'CPU family'
194
- result.cpu_family = data.to_i
195
- when 'Model'
196
- result.model = data.to_i
197
- when 'Model name'
198
- result.model_name = data
199
- when 'Stepping'
200
- result.stepping = data.to_i
201
- when 'CPU MHz'
202
- result.cpu_mhz = data.to_f
203
- when 'CPU max MHz'
204
- result.cpu_max_mhz = data.to_f
205
- when 'CPU min MHz'
206
- result.cpu_min_mhz = data.to_f
207
- when 'CPUBogoMIPS'
208
- result.bogo_mips = data.to_f
209
- when 'Virtualization'
210
- result.virtualization = data
211
- when 'Hypervisor vendor'
212
- result.hypervisor_vendor = data
213
- when 'Virtualization type'
214
- result.virtualization_type = data
215
- when 'L1d cache'
216
- result.l1d_cache = data
217
- when 'L1i cache'
218
- result.l1i_cache = data
219
- when 'L2 cache'
220
- result.l2_cache = data
221
- when 'L3 cache'
222
- result.l3_cache = data
223
- when /^Numa node/
224
- result.numa_nodes << data.split(',')
225
- when /^Vulnerability/
226
- name = field.split('Vulnerability ')[1]
227
- result.vulnerabilities << CPUArchitectureVulnerability.new(name, data)
228
- when 'Flags'
229
- result.flags = data.split
230
- end
231
- end
232
-
233
- result
234
- end
235
- end
236
-
237
- def cpu_info(spec)
238
- Kanrisuru.logger.info do
239
- 'DEPRECATION WARNING: cpu_info will be removed in the upcoming major release. Use lscpu instead.'
240
- end
241
-
242
- name =
243
- case spec
244
- when 'sockets'
245
- '^Socket'
246
- when 'cores_per_socket'
247
- '^Core'
248
- when 'threads'
249
- '^Thread'
250
- when 'cores'
251
- '^CPU('
252
- else
253
- return
254
- end
255
-
256
- command = Kanrisuru::Command.new("lscpu | grep -i '#{name}' | awk '{print $NF}'")
257
- execute(command)
258
-
259
- Kanrisuru::Result.new(command, &:to_i)
260
- end
261
-
262
- def load_average
263
- command = Kanrisuru::Command.new("cat /proc/loadavg | awk '{print $1,$2,$3}'")
264
- execute(command)
265
-
266
- Kanrisuru::Result.new(command) do |cmd|
267
- cmd.to_s.split.map(&:to_f)
268
- end
269
- end
270
-
271
- def free(type)
272
- conversions = {
273
- total: 'MemTotal',
274
- free: 'MemFree',
275
- swap: 'SwapTotal',
276
- swap_free: 'SwapFree'
277
- }
278
-
279
- option = conversions[type.to_sym]
280
- raise ArgumentError, 'Invalid mem type' unless option
281
-
282
- command = Kanrisuru::Command.new("cat /proc/meminfo | grep -i '^#{option}' | awk '{print $2}'")
283
- execute(command)
284
-
285
- ## In kB
286
- Kanrisuru::Result.new(command, &:to_i)
287
- end
288
-
289
- def last(opts = {})
290
- command =
291
- if opts[:failed_attempts]
292
- Kanrisuru::Command.new('lastb')
293
- else
294
- Kanrisuru::Command.new('last')
295
- end
296
-
297
- command.append_flag('-i')
298
- command.append_flag('-F')
299
- command.append_arg('-f', opts[:file])
300
-
301
- ## Some systems only use 1 space between user and TTY field
302
- ## Add an additional space in output formatting for simple parsing
303
- ## logic.
304
- command | "sed 's/ / /'"
305
-
306
- execute_shell(command)
307
-
308
- Kanrisuru::Result.new(command) do |cmd|
309
- lines = cmd.to_a
310
-
311
- mapping = {}
312
-
313
- lines.each do |line|
314
- next if Kanrisuru::Util.blank?(line)
315
- next if line.include?('wtmp') || line.include?('btmp')
316
-
317
- line = line.gsub(' still logged in', '- still logged in') if line.include?('still logged in')
318
-
319
- values = line.split(/\s{2,}/, 4)
320
- user = values[0]
321
- tty = values[1]
322
- ip = IPAddr.new(values[2])
323
-
324
- date_range = values[3]
325
- login, logout = date_range.split(' - ')
326
-
327
- login = parse_last_date(login) if login
328
-
329
- logout = parse_last_date(logout) if logout
330
-
331
- detail = SessionDetail.new
332
- detail.tty = tty
333
- detail.ip_address = ip
334
- detail.login_at = login
335
- detail.logout_at = logout
336
-
337
- detail.success = !Kanrisuru::Util.present?(opts[:failed_attemps])
338
-
339
- mapping[user] = LoginUser.new(user, []) unless mapping.key?(user)
340
-
341
- mapping[user].sessions << detail
342
- end
343
-
344
- mapping.values
345
- end
346
- end
347
-
348
- def ps(opts = {})
349
- group = opts[:group]
350
- user = opts[:user]
351
- pid = opts[:pid]
352
- ppid = opts[:ppid]
353
- # tree = opts[:tree]
354
-
355
- command = Kanrisuru::Command.new('ps ww')
356
-
357
- if !user.nil? || !group.nil? || !pid.nil? || !ppid.nil?
358
- command.append_arg('--user', user.instance_of?(Array) ? user.join(',') : user)
359
- command.append_arg('--group', group.instance_of?(Array) ? group.join(',') : group)
360
- command.append_arg('--pid', pid.instance_of?(Array) ? pid.join(',') : pid)
361
- command.append_arg('--ppid', ppid.instance_of?(Array) ? ppid.join(',') : ppid)
362
- else
363
- command.append_flag('ax')
364
- end
365
-
366
- command.append_arg('-o', 'uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd')
367
- command.append_flag('--no-headers')
368
-
369
- execute(command)
370
-
371
- ## Have found issues with regular newline parsing from command
372
- ## most likely a buffer overflow from SSH buffer.
373
- ## Join string then split by newline.
374
- Kanrisuru::Result.new(command) do |cmd|
375
- result_string = cmd.raw_result.join
376
- rows = result_string.split("\n")
377
-
378
- build_process_list(rows)
379
- end
380
- end
381
-
382
- def kill(signal, pids)
383
- raise ArgumentError, 'Invalid signal' unless Kanrisuru::Util::Signal.valid?(signal)
384
-
385
- ## Use named signals for readabilitiy
386
- signal = Kanrisuru::Util::Signal[signal] if signal.instance_of?(Integer)
387
-
388
- command = Kanrisuru::Command.new('kill')
389
- command << "-#{signal}"
390
- command << (pids.instance_of?(Array) ? pids.join(' ') : pids)
391
-
392
- execute_shell(command)
393
-
394
- Kanrisuru::Result.new(command)
395
- end
396
-
397
- def lsof(_opts = {})
398
- command = Kanrisuru::Command.new('lsof -F pcuftDsin')
399
-
400
- execute_shell(command)
401
- Kanrisuru::Result.new(command) do |cmd|
402
- lines = cmd.to_a
403
-
404
- current_row = nil
405
- current_pid = nil
406
- current_user = nil
407
- current_command = nil
408
-
409
- rows = []
410
-
411
- lines.each do |line|
412
- case line
413
- when /^p/
414
- current_pid = parse_lsof(line, 'p').to_i
415
- when /^c/
416
- current_command = parse_lsof(line, 'c')
417
- when /^u/
418
- current_user = parse_lsof(line, 'u').to_i
419
- when /^f/
420
- rows << current_row if current_row
421
-
422
- current_row = OpenFile.new
423
- current_row.pid = current_pid
424
- current_row.command = current_command
425
- current_row.uid = current_user
426
-
427
- current_row.file_descriptor = parse_lsof(line, 'f')
428
- when /^t/
429
- current_row.type = parse_lsof(line, 't')
430
- when /^D/
431
- current_row.device = parse_lsof(line, 'D')
432
- when /^s/
433
- current_row.fsize = parse_lsof(line, 's').to_i
434
- when /^i/
435
- current_row.inode = parse_lsof(line, 'i').to_i
436
- when /^n/
437
- current_row.name = parse_lsof(line, 'n')
438
- end
439
- end
440
-
441
- rows << current_row if current_row
442
-
443
- rows
444
- end
445
- end
446
-
447
- def kstat
448
- kernel_statistics
449
- end
450
-
451
- def kernel_statistics
452
- command = Kanrisuru::Command.new('cat /proc/stat')
453
-
454
- execute_shell(command)
455
-
456
- Kanrisuru::Result.new(command) do |cmd|
457
- lines = cmd.to_a
458
-
459
- result = KernelStatistic.new
460
- result.cpus = []
461
-
462
- lines.each do |line|
463
- values = line.split
464
- field = values[0]
465
- values = values[1..-1].map(&:to_i)
466
-
467
- case field
468
- when /^cpu/
469
- cpu_stat = KernelStatisticCpu.new
470
- cpu_stat.user = values[0]
471
- cpu_stat.nice = values[1]
472
- cpu_stat.system = values[2]
473
- cpu_stat.idle = values[3]
474
- cpu_stat.iowait = values[4]
475
- cpu_stat.irq = values[5]
476
- cpu_stat.softirq = values[6]
477
- cpu_stat.steal = values[7]
478
- cpu_stat.guest = values[8]
479
- cpu_stat.guest_nice = values[9]
480
-
481
- case field
482
- when /^cpu$/
483
- result.cpu_total = cpu_stat
484
- when /^cpu\d+/
485
- result.cpus << cpu_stat
486
- end
487
- when 'intr'
488
- result.interrupt_total = values[0]
489
- result.interrupts = values[1..-1]
490
- when 'softirq'
491
- result.softirq_total = values[0]
492
- result.softirqs = values[1..-1]
493
- else
494
- result[field] = values[0]
495
- end
496
- end
497
-
498
- result
499
- end
500
- end
501
-
502
- def uptime
503
- ## Ported from https://github.com/djberg96/sys-uptime
504
- command = Kanrisuru::Command.new('cat /proc/uptime')
505
-
506
- execute_shell(command)
507
-
508
- Kanrisuru::Result.new(command) do |cmd|
509
- seconds = cmd.to_s.split[0].to_i
510
- minutes = seconds / 60
511
- hours = seconds / 3600
512
- days = seconds / 86_400
513
-
514
- seconds_dur = seconds
515
- days_dur = seconds_dur / 86_400
516
- seconds_dur -= days_dur * 86_400
517
- hours_dur = seconds_dur / 3600
518
- seconds_dur -= hours_dur * 3600
519
- minutes_dur = seconds_dur / 60
520
- seconds_dur -= minutes_dur * 60
521
- uptime_s = "#{days_dur}:#{hours_dur}:#{minutes_dur}:#{seconds_dur}"
522
-
523
- boot_time = Time.now - seconds
524
-
525
- Uptime.new(
526
- boot_time,
527
- uptime_s,
528
- seconds,
529
- minutes,
530
- hours,
531
- days
532
- )
533
- end
534
- end
535
-
536
- def w(opts = {})
537
- users = opts[:users]
538
- command = Kanrisuru::Command.new('w -hi')
539
-
540
- command << users if Kanrisuru::Util.present?(users)
541
-
542
- execute(command)
543
-
544
- Kanrisuru::Result.new(command) do |cmd|
545
- result_string = cmd.raw_result.join
546
- rows = result_string.split("\n")
547
-
548
- rows.map do |row|
549
- values = *row.split(/\s+/, 8)
550
- UserLoggedIn.new(
551
- values[0],
552
- values[1],
553
- IPAddr.new(values[2]),
554
- values[3],
555
- values[4],
556
- values[5].to_f,
557
- values[6].to_f,
558
- values[7]
559
- )
560
- end
561
- end
562
- end
563
-
564
- def who(opts = {})
565
- w(opts)
566
- end
567
-
568
- def reboot(opts = {})
569
- time = opts[:time]
570
- cancel = opts[:cancel]
571
- message = opts[:message]
572
- no_wall = opts[:no_wall]
573
-
574
- command = Kanrisuru::Command.new('shutdown')
575
-
576
- if Kanrisuru::Util.present?(cancel)
577
- command.append_flag('-c')
578
- command << message if message
579
- else
580
- command.append_flag('-r')
581
-
582
- time = format_shutdown_time(time) if Kanrisuru::Util.present?(time)
583
-
584
- if Kanrisuru::Util.present?(no_wall)
585
- command.append_flag('--no-wall')
586
- else
587
- command << time if time
588
- command << message if message
589
- end
590
- end
591
-
592
- begin
593
- execute_shell(command)
594
- Kanrisuru::Result.new(command)
595
- rescue IOError
596
- ## When rebooting 'now', ssh io stream closes
597
- ## Set exit status to 0
598
- command.handle_status(0)
599
- Kanrisuru::Result.new(command)
600
- end
601
- end
602
-
603
- def poweroff(opts = {})
604
- time = opts[:time]
605
- cancel = opts[:cancel]
606
- message = opts[:message]
607
- no_wall = opts[:no_wall]
608
-
609
- command = Kanrisuru::Command.new('shutdown')
610
-
611
- if Kanrisuru::Util.present?(cancel)
612
- command.append_flag('-c')
613
- command << message if message
614
- else
615
- time = format_shutdown_time(time) if Kanrisuru::Util.present?(time)
616
-
617
- if Kanrisuru::Util.present?(no_wall)
618
- command.append_flag('--no-wall')
619
- else
620
- command << time if time
621
- command << message if message
622
- end
623
- end
624
-
625
- begin
626
- execute_shell(command)
627
-
628
- Kanrisuru::Result.new(command)
629
- rescue IOError
630
- ## When powering off 'now', ssh io stream closes
631
- ## Set exit status to 0
632
- command.handle_status(0)
633
-
634
- Kanrisuru::Result.new(command)
635
- end
636
- end
637
-
638
- private
639
-
640
- def parse_lsof(line, char)
641
- line.split(char, 2)[1]
642
- end
643
-
644
- def parse_last_date(string)
645
- tokens = string.split
646
-
647
- return if tokens.length < 4
648
-
649
- month_abbr = tokens[1]
650
- day = tokens[2]
651
- timestamp = tokens[3]
652
- year = tokens[4]
653
-
654
- DateTime.parse("#{day} #{month_abbr} #{year} #{timestamp}")
655
- end
656
-
657
- def parse_policy_abbr(value)
658
- case value
659
- when '-'
660
- 'not reported'
661
- when 'TS'
662
- 'SCHED_OTHER'
663
- when 'FF'
664
- 'SCHED_FIFO'
665
- when 'RR'
666
- 'SCHED_RR'
667
- when 'B'
668
- 'SCHED_BATCH'
669
- when 'ISO'
670
- 'SCHED_ISO'
671
- when 'IDL'
672
- 'SCHED_IDLE'
673
- when 'DLN'
674
- 'SCHED_DEADLINE'
675
- else
676
- 'unknown value'
677
- end
678
- end
679
-
680
- def build_process_list(rows)
681
- rows.map do |row|
682
- values = *row.split(/\s+/, 15)
683
- values.shift if values[0] == ''
684
-
685
- ProcessInfo.new(
686
- values[0].to_i,
687
- values[1],
688
- values[2].to_i,
689
- values[3],
690
- values[4].to_i,
691
- values[5].to_i,
692
- values[6].to_f,
693
- values[7].to_f,
694
- values[8],
695
- values[9].to_i,
696
- values[10].to_i,
697
- values[11],
698
- parse_policy_abbr(values[11]),
699
- values[12],
700
- values[13]
701
- )
702
- end
703
- end
704
-
705
- def format_shutdown_time(time)
706
- if time.instance_of?(Integer) || !/^[0-9]+$/.match(time).nil?
707
- "+#{time}"
708
- elsif !/^[0-9]{0,2}:[0-9]{0,2}$/.match(time).nil? || time == 'now'
709
- time
710
- else
711
- raise ArgumentError, 'Invalid time format'
712
- end
713
- end
714
31
  end
715
32
  end
716
33
  end