728x90
반응형

개요

그룹웨어 DB인 Postgresql에서 User View Table에 임직원이 추가 되거나 부서 이동 하였을 경우

그룹웨어에 먼저 업데이트 되는데 이 데이터를 긁어와서 LDAP에도 자동으로 업데이트 되도록 List를 추출하는 작업이다

Python으로 임직원 목록을 불러오며, 외주직원은 제외하거나, 퇴사한 직원, 전체 부서 리스트 등을 추출 한다.

#-*- coding: euc-kr -*-
import boto3
import json
import psycopg2
import requests
import sys
import chardet

sys.stdout = open('/usr/share/openldap-servers/userList.json', 'w')

try:
    conn_string = "host='gw.testhub.com' dbname='tims' user='test' password='test2164!@' port='5432'"
    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()
    #qry_origin = "select userid, status, username, mailaddr, deptname, updated_at from view_user where updated_at >= current_timestamp + '-240 minute' and deptcode not like 'E%' and order by updated_at desc"
    qry_origin = "select userid, status, username, mailaddr, deptname, updated_at from view_user where deptcode not like 'E%'"
    qry = u"select row_to_json(tmp) from ("+ qry_origin +") tmp;"
    cur.execute(qry)
    rownum = int(cur.rowcount)
    i=0
    while i < rownum:
        result = cur.fetchone()[0]
        print(json.dumps(result, ensure_ascii=False).encode('utf8'))
        i = i + 1
    cur.close()
    conn.close()
except psycopg2.DatabaseError as db_err:
    print('!!! not connected !!!', db_err)

sys.stdout.close()

sys.stdout = open('/usr/share/openldap-servers/userStopList.json', 'w')

try:
    conn_string = "host='gw.testhub.com' dbname='tims' user='test' password='test2164!@' port='5432'"
    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()
    #qry_origin = "select userid, status, username, mailaddr, updated_at from view_user where updated_at >= current_timestamp + '-240 minute' and status='STOP'"
    qry_origin = "select userid, status, username, mailaddr, updated_at from view_user where status='STOP'"
    qry = u"select row_to_json(tmp) from ("+ qry_origin +") tmp;"
    cur.execute(qry)
    rownum = int(cur.rowcount)
    i=0
    while i < rownum:
        result = cur.fetchone()[0]
        print(json.dumps(result, ensure_ascii=False).encode('utf8'))
        i = i + 1
    cur.close()
    conn.close()
except psycopg2.DatabaseError as db_err:
    print('!!! not connected !!!', db_err)

sys.stdout.close()

sys.stdout = open('/usr/share/openldap-servers/gwTotalGroupList.lst', 'w')

try:
    conn_string = "host='gw.testhub.com' dbname='tims' user='test' password='test2164!@' port='5432'"
    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()
    qry_origin = "select distinct deptcode, deptname from view_user where deptcode not like 'E%'"
    qry = u"select row_to_json(tmp) from ("+ qry_origin +") tmp;"
    cur.execute(qry)
    rownum = int(cur.rowcount)
    i=0
    while i < rownum:
        result = cur.fetchone()[0]
        print(json.dumps(result, ensure_ascii=False).encode('utf8'))
        i = i + 1
    cur.close()
    conn.close()
except psycopg2.DatabaseError as db_err:
    print('!!! not connected !!!', db_err)

sys.stdout.close()

Lambda로도 비슷하게 가능하다.

import boto3
import json
import psycopg2 #PostgreSQL 과 연결하려면 반드시 있어야 되는 드라이버 (람다에서는 레이어 붙이는걸로 처리 가능)
import requests

def lambda_handler(event, context):
    
    print('db connect')
    conn = psycopg2.connect(host='gw.testhub.com', dbname='tims', user='test', password='test2164!@', port='5432') #DB 접속 구문 
    print('db connect success!')
    cur = conn.cursor() #커서 생성 (쿼리 칠 때 필요함)
    print('start query')
    
    qry_origin = "select status, empno, username, mailaddr, deptcode, deptname, dutycode, positionname, dutyname, updated_at from view_user where updated_at >= current_timestamp + '-10 minute' and status like 'STOP' and dutyname is null order by updated_at desc"
    qry = "select row_to_json(tmp) from ("+ qry_origin +") tmp;" #쿼리문 (row_to_json으로 바로 JSON 형식으로 뽑아다줌)
    
    cur.execute(qry) #쿼리 실행
    rownum = int(cur.rowcount)  # DB 총 행 개수 구하기
    print(rownum)
    
    i=0
    while i < rownum: #쿼리 결과 값 나온 행 수 만큼 반복해서 
        result = cur.fetchone()[0] #cur.fetchone()로 한 행 결과값 묶어주기 (fetchall() 을 호출하면 전체 행 값이 나옴) ([0] 을 넣어줘야 대괄호나 괄호가 안생김)
        req_url = requests.post('https://intra.testhub.com:7075/RF_COUNT/minsu/insertUsr', json = result) #결과 값 URL 호출
        print(result)
        print(req_url)
        i = i + 1
   

    cur.close()
    conn.close()
728x90
300x250
728x90
반응형

PostgreSQL 문에서 Select으로 리스트를 뽑을 때 Json 형식으로 Row를 Export 하는 방식이다.

row_to_json으로 사용하는 것으로 생각하면 된다.

특정 알파뱃 제외 구문이나 인코딩 방식 등 포함 되어 있으니 보고하실거면 주의 필요함

#-*- coding: euc-kr -*-
import json
import psycopg2
import requests
import sys
import chardet

sys.stdout = open('/usr/share/openldap-servers/userList.json', 'w')

try:
    conn_string = "host='dbsvr.com' dbname='gooddb' user='test' password='test12!@' port='5432'"
    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()
    qry_origin = "select userid, status, empno, username, mailaddr, dutycode, positionname, deptcode, deptname, dutyname, updated_at from view_user where updated_at >= current_timestamp + '-120 minute' and deptcode not like 'E%' and dutyname is not null order by updated_at desc"
    #qry_origin = "select userid, status, empno, username, mailaddr, dutycode, positionname, deptcode, deptname, dutyname, updated_at from view_user where status='ONLINE'
    qry = u"select row_to_json(tmp) from ("+ qry_origin +") tmp;"
    cur.execute(qry)
    rownum = int(cur.rowcount)
    i=0
    while i < rownum:
        result = cur.fetchone()[0]
        print(json.dumps(result, ensure_ascii=False).encode('utf8'))
        i = i + 1
    cur.close()
    conn.close()
except psycopg2.DatabaseError as db_err:
    print('!!! not connected !!!', db_err)

sys.stdout.close()

sys.stdout = open('/usr/share/openldap-servers/userStopList.json', 'w')

try:
    conn_string = "host='dbsvr.com' dbname='gooddb' user='test' password='test12!@' port='5432'"
    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()
    qry_origin = "select userid, status, empno, username, mailaddr, dutycode, positionname, deptcode, deptname, dutyname from view_user where status='STOP'"
    qry = u"select row_to_json(tmp) from ("+ qry_origin +") tmp;"
    cur.execute(qry)
    rownum = int(cur.rowcount)
    i=0
    while i < rownum:
        result = cur.fetchone()[0]
        print(json.dumps(result, ensure_ascii=False).encode('utf8'))
        i = i + 1
    cur.close()
    conn.close()
except psycopg2.DatabaseError as db_err:
    print('!!! not connected !!!', db_err)

sys.stdout.close()
728x90
300x250
728x90
반응형

1. cc complier 로 .so 생성 하기

[www@alpha clib]$ gcc -c -I/usr/include -I/usr/local/include/python3.6m -fPIC test_show_video.c
[www@alpha clib]$ ls
Makefile  test_show.py  test_show_video.c  test_show_video.o
[www@alpha clib]$ gcc -shared -fPIC -o test_show_video.so test_show_video.o
[www@alpha clib]$ ls
Makefile      test_show_video.c  test_show_video.so
test_show.py  test_show_video.o

 

2. .so 를 python 에서 import 하기 위한 모듈화 시키기

[www@alpha clib]$ vi setup.py
# setup.py
from distutils.core import setup, Extension
setup(name = "test_show_video",
version = "1.0",
description = "name",
author = "Samsjang",
author_email = "test@naver.com",  ## 회사 이메일
url = "http://www.naver.com",  ## 회사 url
ext_modules = [Extension("test_show_video", ["test_show_video.c"])]
)
[www@alpha clib]$ python setup.py install
running install
running build
running build_ext
building 'test_show_video' extension
creating build
creating build/temp.linux-x86_64-3.6
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -I/opt/rh/rh-python36/root/usr/include -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/www/test4/include -I/opt/rh/rh-python36/root/usr/include/python3.6m -c test_show_video.c -o build/temp.linux-x86_64-3.6/test_show_video.o
creating build/lib.linux-x86_64-3.6
gcc -pthread -shared -L/opt/rh/rh-python36/root/usr/lib64-Wl,-z,relro -Wl,-rpath,/opt/rh/rh-python36/root/usr/lib64 -Wl,--enable-new-dtags build/temp.linux-x86_64-3.6/test_show_video.o -L/opt/rh/rh-python36/root/usr/lib64 -lpython3.6m -o build/lib.linux-x86_64-3.6/test_show_video.cpython-36m-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.6/test_show_video.cpython-36m-x86_64-linux-gnu.so -> /home/www/test4/lib64/python3.6/site-packages
running install_egg_info
Writing /home/www/test4/lib64/python3.6/site-packages/test_show_video-1.0-py3.6.egg-info
[www@alpha clib]$ !vi

 

3. 사용하기

[www@alpha clib]$ vi test_show.py
import test_show_video as sv
sv.on_videocam()
728x90
300x250

'IT > Misc Tip' 카테고리의 다른 글

DNS Lookup이나 Global Ping체크 시  (0) 2021.08.19
svn충돌 해결방법 (conflict문제)  (0) 2021.08.12
Mac에서 ansible 수행시 발생된 에러  (0) 2021.08.03
DNS 캐시 클리어  (0) 2021.07.30
CentOS7 Python3.6 & Django Install  (0) 2021.07.27

+ Recent posts