-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcid_disableuser_v0.1
executable file
·149 lines (118 loc) · 5.38 KB
/
hcid_disableuser_v0.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
import csv
import pandas as pd
import numpy as np
import time
import sys
import random
import scpclient
import md5
import subprocess
import os
import socket
import smtplib
from pandas import DataFrame
from sqlalchemy import create_engine
#pd.show_versions()
# Golbal Variable
datenow=(time.strftime("%Y-%m-%d"))
dirname=(time.strftime("%Y-%m-%d"))
OUT_ADEX='/SMART/DISABLE/OUT/AD_CLOSED/'
OUT_ADEX_FIX='/SMART/DISABLE/OUT/AD_FIX/'
OUT_ZM='/SMART/DISABLE/OUT/ZM_CLOSED/'
OUT_DB='/SMART/DISABLE/OUT/DB_COMPARE/'
ARCHIVE_AD='/SMART/DISABLE/ARCHIVE/AD/'
ARCHIVE_ZM='/SMART/DISABLE/ARCHIVE/ZM/'
ARCHIVE_ZM_TODAY=ARCHIVE_ZM+dirname+'/'
ARCHIVE_AD_TODAY=ARCHIVE_AD+dirname+'/'
ZM_SVR="10.58.122.209"
# -------------------- create folder every day
try :
# create directory in each OUT path
os.mkdir(OUT_DB+dirname)
except :
print("Path Created")
try :
# create directory in each OUT path
os.mkdir(ARCHIVE_AD+dirname)
except :
print("Path Created")
try :
# create directory in each OUT path
os.mkdir(ARCHIVE_ZM+dirname)
except :
print("Path Created")
#FILE STORE
ADEX_FILE=OUT_ADEX+'ad_disable-'+datenow+'.csv'
ZM_FILE=OUT_ZM+'zimbra_disable-'+datenow+'.csv'
IMPORT_DB=OUT_DB + dirname+'/'+'importdb_disable-'+datenow+'.csv'
#-- DB Engine
engine2 = create_engine("mysql://root:paganini@localhost/homecredit")
#dataframe database_check_disable
query_disable = 'select nik,d_name,upn,email,ad_feature,date_closed from tbl_users where status_employee = 1 and status_email = 1 and DATE(date_closed) = CURDATE();'
source_db = pd.read_sql(query_disable, engine2)
source_db.columns=['EmployeeNo','DisplayName','UPN','email','ad_feature','date_closed']
source_db['UserLogonName'] = source_db.UPN.str.split("@").str[0]
#Add date Created column
date_field = datenow
source_db['date_deleted'] = date_field
#Add new column for zimbra mandatory
source_db['zmprov']='zmprov'
source_db['ma']='ma'
source_db['zimbraAccountStatus']='zimbraAccountStatus'
source_db['closed']='closed'
# SPESIFY ACCOUNT BASED ON SYSTEM
AD = source_db.loc[(source_db.EmployeeNo.notnull()) & (source_db.ad_feature == "y")]
ZIMBRA = source_db.loc[(source_db.email.str.contains('ext.homecredit.co.id'))]
# EXPORT to csv create zimbra
# Generate file for creating file zimbra
CONV_AD = AD.astype('str').replace('\.0', '', regex=True)
CONV_ZM = ZIMBRA.astype('str').replace('\.0', '', regex=True)
# FORMAT CREATE ZIMBRA
# zmprov ma user@domain.tld zimbraAccountStatus closed
zmfile = open(ZM_FILE,'w+')
CONV_ZM.to_csv(ZM_FILE,encoding='utf-8',sep=' ', index = None, header=None,mode = 'a', columns=['zmprov','ma','email','zimbraAccountStatus','closed'])
#Main Table AD Format
# Export to csv Untuk AD format dengan Exchange
tbl_adex=['EmployeeNo','DisplayName','UserLogonName']
CONV_AD.to_csv(ADEX_FILE, sep=',', encoding='utf-8', index = None, header=tbl_adex,columns=['EmployeeNo','DisplayName','UserLogonName'])
# ------------------------------------- SEND TO SSH and Email for result file
with open(ZM_FILE) as zimbra_file:
first = zimbra_file.read(1)
if not first:
print('\nZimbra Not Process:\nNot sending any Files, zimbra file is empty..\n')
else:
# send file zimbra to server zimbra - SSH
os.system("rsync -avh " +ZM_FILE+ " zimbra@"+ZM_SVR+":/opt/zimbra/script_zimbra/smart_ams/run/disable/")
# archive file before remove
os.system("cp -r " +ZM_FILE+" "+ARCHIVE_ZM_TODAY)
# ----------- Notification to user
zm_file = pd.read_csv(ZM_FILE)
print(zm_file)
if not zm_file.empty or os.stat(ZM_FILE).st_size > 0:
# -- send mail
os.system("echo 'Hi Team, \n\nWe could inform you we will disable account automatically based on attachment, please be aware and change in ams if this user still active. \n\nThanks\n\n IT Servers '| mailx -v -r 'amsnew@homecredit.co.id' -s 'Notif disable Zimbra account "+datenow+"' -a "+ZM_FILE+" -S smtp=smtp1-int.id.prod doni.hirmansyah01@homecredit.co.id firmandha.noerdiansya@homecredit.co.id")
else:
print ('\nZimbra :\nZimbra file is empty, File Not Process....\n')
# remove zimbra file
os.system("rm -rf "+ZM_FILE+ ";echo 'file "+ZM_FILE+" success and file aready removed..!'")
print('\nZimbra Proceed :\nZimbra File already send to remote server\n')
with open(ADEX_FILE) as adex_file:
first = adex_file.read(1)
if not first:
print('\nAdex Not Process:\nNot sending any Files, zimbra file is empty..\n')
else:
# send file zimbra to server zimbra - SSH
#os.system("rsync -avh " +ZM_FILE+ " zimbra@"+ZM_SVR+":/opt/zimbra/script_zimbra/smart_ams/run/disable/")
# Move to FIX file
os.system("rm -rf "+OUT_ADEX_FIX+"/*.csv"+ ";echo 'file success and file aready removed..!'")
os.system("cp -r " +ADEX_FILE+" "+OUT_ADEX_FIX)
## -- notification auto disable everyday
adex_file = pd.read_csv(ADEX_FILE)
if not adex_file.empty:
# -- send mail
os.system("echo 'Hi Team, \n\nWe could inform you we will disable account automatically based on attachment, please be aware and change in ams if this user still active. \n\nThanks\n\n IT Servers '| mailx -v -r 'amsnew@homecredit.co.id' -s 'Notif disable AD Account "+datenow+"' -a "+ADEX_FILE+" -S smtp=smtp1-int.id.prod doni.hirmansyah01@homecredit.co.id firmandha.noerdiansya@homecredit.co.id")
else:
print ('\nAD and Exchange Process :\nAD file is empty, File Not Process....\n')
# archive file before remove
os.system("cp -r " +ADEX_FILE+" "+ARCHIVE_AD_TODAY)