質問編集履歴

10

解決したので追記

2018/01/10 15:19

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,7 @@
1
+ 1/10/2018 解決:SymantecEndpointProtectionのバグによりゾンビプロセスが発生していたことがRHELとSymantecのサポート解析により判明しました。現在は対象サーバー上でSEPのサービスを停止した所、ゾンビプロセスが0となりました、現在SEPのパッチ適用調整中です。修正したPythonスクリプトはゾンビプロセスも生成せずに完璧にメモリ開放してくれる事を確認しました。nmyu様改めてありがとうございました!
2
+
3
+
4
+
1
5
  nmyu様の以下ご教授により、Pythonスクリプト自体の問題点が修正され、スクリプト実行により生成されていた、ゾンビプロセス発生は抑える事ができてたと考えております。スクリプト実行されていない状況で一晩モニターした結果、200から300程度のゾンビプロセスが発生していたので、
2
6
 
3
7
  現在はRHELのサポートにもサポートケースを作成し、他の要因も調査しております。

9

その後の進捗アップデート

2018/01/10 15:19

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,10 @@
1
+ nmyu様の以下ご教授により、Pythonスクリプト自体の問題点が修正され、スクリプト実行により生成されていた、ゾンビプロセス発生は抑える事ができてたと考えております。スクリプト実行されていない状況で一晩モニターした結果、200から300程度のゾンビプロセスが発生していたので、
2
+
1
- 実行する度ゾンビプロセスを成するPythonスクリプト
3
+ 現在はRHELのサポートもサポートケースをし、他の要因も調査しておりま
4
+
5
+
6
+
2
-
7
+ ------------
3
-
4
8
 
5
9
  子プロセスがSSH接続のために生成したソケットを適切にクローズできていないのではないかと考えています。スクリプトの定期実行を自動化した場合、いずれメモリを食いつぶしてしまうため対策を調べております。
6
10
 

8

修正版コードの表記修正

2018/01/09 04:58

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -150,7 +150,7 @@
150
150
 
151
151
      #ここで変数hの循環参照を起こしていた
152
152
 
153
- print('## SSH connection failed for %s ##' % self.host + '\n')
153
+ print('## SSH connection failed for %s ##' % h + '\n')
154
154
 
155
155
 
156
156
 
@@ -260,7 +260,7 @@
260
260
 
261
261
  return lines
262
262
 
263
- print('## %s SSH connection failed ##' % h + '\n')
263
+ print('## %s SSH connection failed ##' % self.host + '\n')
264
264
 
265
265
 
266
266
 

7

nmyu様のご指摘にそってコード修正

2018/01/08 16:05

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -146,7 +146,11 @@
146
146
 
147
147
  return lines
148
148
 
149
+
150
+
151
+     #ここで変数hの循環参照を起こしていた
152
+
149
- print('## SSH connection failed for %s ##' % h + '\n')
153
+ print('## SSH connection failed for %s ##' % self.host + '\n')
150
154
 
151
155
 
152
156
 

6

表記修正

2018/01/08 15:49

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -186,8 +186,6 @@
186
186
 
187
187
  ```ここに言語を入力
188
188
 
189
- ```ここに言語を入力
190
-
191
189
  from contextlib import suppress, closing
192
190
 
193
191
 
@@ -316,6 +314,6 @@
316
314
 
317
315
  main()
318
316
 
317
+
318
+
319
319
  ```
320
-
321
- ```

5

umyu様のアドバイスに従い、main()を定義

2018/01/08 15:23

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -186,6 +186,8 @@
186
186
 
187
187
  ```ここに言語を入力
188
188
 
189
+ ```ここに言語を入力
190
+
189
191
  from contextlib import suppress, closing
190
192
 
191
193
 
@@ -194,6 +196,34 @@
194
196
 
195
197
 
196
198
 
199
+ #Classes and Functions
200
+
201
+ class InputReader:
202
+
203
+ def __init__(self, commands_path, hosts_path):
204
+
205
+ self.commands_path = commands_path
206
+
207
+ self.hosts_path = hosts_path
208
+
209
+
210
+
211
+ def read(self):
212
+
213
+ self.commands = self.__readlines(self.commands_path)
214
+
215
+ self.hosts = self.__readlines(self.hosts_path)
216
+
217
+
218
+
219
+ def __readlines(self, path):
220
+
221
+ with open(path) as f:
222
+
223
+ return [v.strip() for v in f.readlines()] #List comprehension
224
+
225
+
226
+
197
227
  class CommandExecuter:
198
228
 
199
229
  def __init__(self, host, command):
@@ -248,36 +278,44 @@
248
278
 
249
279
 
250
280
 
281
+ def main():
282
+
283
+ reader = InputReader("/root/CR/commands.txt", "/root/CR/systems.txt")
284
+
285
+ reader.read()
286
+
287
+
288
+
289
+ for h in reader.hosts:
290
+
291
+ for c in reader.commands:
292
+
293
+ executer = CommandExecuter(h, c)
294
+
295
+ results = executer.execute()
296
+
297
+ print("IP: {0} :({1}):".format(h, c) + '\n')
298
+
299
+ if results != None:
300
+
301
+ for i in results:
302
+
303
+ print(i + '\n')
304
+
305
+ del executer
306
+
307
+ del results
308
+
309
+ gc.collect()
310
+
311
+
312
+
251
313
  #Main Procedure
252
314
 
253
315
  if __name__ == '__main__':
254
316
 
255
- reader = InputReader("commands.txt", "systems.txt")
256
-
257
- reader.read()
317
+ main()
258
-
259
-
260
-
261
- for h in reader.hosts:
262
-
263
- for c in reader.commands:
264
-
265
- executer = CommandExecuter(h, c)
266
-
267
- results = executer.execute()
268
-
269
- print("IP: {0} :({1}):".format(h, c) + '\n')
270
-
271
- if results != None:
272
-
273
- for i in results:
274
-
275
- print(i + '\n')
276
-
277
- del executer
278
-
279
- del results
280
-
281
- gc.collect()
282
318
 
283
319
  ```
320
+
321
+ ```

4

タグ修正

2018/01/08 15:22

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
File without changes

3

SSH Exceptionに対応したPrint関数のインデント修正

2018/01/08 13:27

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -228,7 +228,7 @@
228
228
 
229
229
  return lines
230
230
 
231
- print('## %s SSH connection failed ##' % h + '\n')
231
+ print('## %s SSH connection failed ##' % h + '\n')
232
232
 
233
233
 
234
234
 

2

umyuさんのアドバイスに従って再修正

2018/01/08 13:14

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -186,6 +186,14 @@
186
186
 
187
187
  ```ここに言語を入力
188
188
 
189
+ from contextlib import suppress, closing
190
+
191
+
192
+
193
+ ~~~~途中省略
194
+
195
+
196
+
189
197
  class CommandExecuter:
190
198
 
191
199
  def __init__(self, host, command):
@@ -200,36 +208,42 @@
200
208
 
201
209
  with suppress(Exception):
202
210
 
203
- #with suppress(TimeoutError):
204
-
205
- ssh = paramiko.SSHClient()
211
+ with closing(paramiko.SSHClient()) as ssh:
206
-
212
+
207
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
213
+ ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
208
-
214
+
209
- ssh.connect(self.host, username=USER, password=PSWD)
215
+ ssh.connect(self.host, username=USER, password=PSWD)
210
-
211
-
212
-
216
+
217
+
218
+
213
- stdin, stdout, stderr = ssh.exec_command(self.command)
219
+ stdin, stdout, stderr = ssh.exec_command(self.command)
214
-
215
-
216
-
220
+
221
+
222
+
217
- errors = stderr.readlines()
223
+ errors = stderr.readlines()
218
-
219
-
220
-
224
+
225
+
226
+
221
- lines = [v.strip() for v in stdout.readlines()]
227
+ lines = [v.strip() for v in stdout.readlines()]
222
-
228
+
223
- return lines
229
+ return lines
224
-
225
- ssh.close()
230
+
226
-
227
- print('## %s SSH connection failed ##' % h + '\n')
231
+ print('## %s SSH connection failed ##' % h + '\n')
228
232
 
229
233
 
230
234
 
231
235
  def __del__(self):
232
236
 
237
+ self.host = None
238
+
239
+ self.command = None
240
+
241
+ self.commands_path = None
242
+
243
+ self.hosts_path = None
244
+
245
+ self.exec_command = None
246
+
233
247
  del self
234
248
 
235
249
 
@@ -238,7 +252,7 @@
238
252
 
239
253
  if __name__ == '__main__':
240
254
 
241
- reader = InputReader("/root/CR/commands.txt", "/root/CR/systems.txt")
255
+ reader = InputReader("commands.txt", "systems.txt")
242
256
 
243
257
  reader.read()
244
258
 
@@ -264,6 +278,6 @@
264
278
 
265
279
  del results
266
280
 
267
- gc.collect()
281
+ gc.collect()
268
282
 
269
283
  ```

1

アドバイスにそってコード修正

2018/01/08 13:01

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -179,3 +179,91 @@
179
179
 
180
180
 
181
181
  ```
182
+
183
+ コード修正箇所:
184
+
185
+
186
+
187
+ ```ここに言語を入力
188
+
189
+ class CommandExecuter:
190
+
191
+ def __init__(self, host, command):
192
+
193
+ self.host = host
194
+
195
+ self.command = command
196
+
197
+
198
+
199
+ def execute(self):
200
+
201
+ with suppress(Exception):
202
+
203
+ #with suppress(TimeoutError):
204
+
205
+ ssh = paramiko.SSHClient()
206
+
207
+ ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
208
+
209
+ ssh.connect(self.host, username=USER, password=PSWD)
210
+
211
+
212
+
213
+ stdin, stdout, stderr = ssh.exec_command(self.command)
214
+
215
+
216
+
217
+ errors = stderr.readlines()
218
+
219
+
220
+
221
+ lines = [v.strip() for v in stdout.readlines()]
222
+
223
+ return lines
224
+
225
+ ssh.close()
226
+
227
+ print('## %s SSH connection failed ##' % h + '\n')
228
+
229
+
230
+
231
+ def __del__(self):
232
+
233
+ del self
234
+
235
+
236
+
237
+ #Main Procedure
238
+
239
+ if __name__ == '__main__':
240
+
241
+ reader = InputReader("/root/CR/commands.txt", "/root/CR/systems.txt")
242
+
243
+ reader.read()
244
+
245
+
246
+
247
+ for h in reader.hosts:
248
+
249
+ for c in reader.commands:
250
+
251
+ executer = CommandExecuter(h, c)
252
+
253
+ results = executer.execute()
254
+
255
+ print("IP: {0} :({1}):".format(h, c) + '\n')
256
+
257
+ if results != None:
258
+
259
+ for i in results:
260
+
261
+ print(i + '\n')
262
+
263
+ del executer
264
+
265
+ del results
266
+
267
+ gc.collect()
268
+
269
+ ```