-
-
Save forstie/2deda50658106461f650cd71917feff9 to your computer and use it in GitHub Desktop.
-- ================================================================= | |
-- Author: Scott Forstie | |
-- Email : [email protected] | |
-- Date : January 10, 2020 | |
-- ================================================================= | |
-- | |
-- Setup: | |
-- 1) create and populate the spreadsheet generator table | |
-- 2) change the procedure source code: | |
-- 2a) Use ACS to upload acsbundle.jar to /home/acs/acsbundle.jar | |
-- 2b) Change [email protected] to your email address. <==== Please don't email me your spreadsheets :) | |
-- 2c) Change /system=COMMON1 to use the name of your IBM i | |
-- 2d) Change the ADDUSRSMTP user profile to be your user profile | |
-- | |
cl: STRTCPSVR SERVER(*SMTP) ; | |
cl: ADDUSRSMTP USRPRF(SCOTTF); | |
create schema coolstuff; | |
create table coolstuff.spreadsheet_generator | |
(IFS_PATH varchar(1000), | |
Spreadsheet_query varchar(10000) | |
); | |
truncate coolstuff.spreadsheet_generator; | |
insert into coolstuff.spreadsheet_generator | |
values ('/home/SCOTTF/systemnameGroupPTFCurrency', | |
'SELECT * FROM systools.group_ptf_currency'); | |
insert into coolstuff.spreadsheet_generator | |
values ('/home/SCOTTF/top10spool', | |
'select user_name, sum(size) as total_spool_space from qsys2.output_queue_entries_basic group by user_name order by total_spool_space desc limit 10'); | |
insert into coolstuff.spreadsheet_generator | |
values ('/home/SCOTTF/ALLOBJusers', | |
'select authorization_name,status,no_password_indicator,previous_signon,text_description | |
from qsys2.user_info where special_authorities like ''''%*ALLOBJ%'''' or authorization_name in (select user_profile_name | |
from qsys2.group_profile_entries where group_profile_name in (select authorization_name | |
from qsys2.user_info where special_authorities like ''''%*ALLOBJ%'''')) order by authorization_name'); | |
select * from coolstuff.spreadsheet_generator; | |
stop; | |
-- | |
-- 1 time setup: | |
-- ============= | |
-- a) Locate ACS (acsbundle.jar) in the IFS at /home/acs/acsbundle.jar | |
-- b) Update the code below to use the name of your system | |
-- change COMMON1 to your system name | |
-- If you're on IBM i 7.3 or higher, with a current Db2 PTF group, just query QSYS2.SYSTEM_STATUS_INFO. | |
-- select partition_name from qsys2.system_status_info | |
-- | |
create or replace procedure coolstuff.generate_spreadsheets() | |
begin | |
declare cmdtext clob(2k); | |
declare v_cmdstmt varchar(2000); | |
declare v_ifs_path varchar(1000); | |
declare v_ifs_full_path varchar(2000); | |
declare v_spreadsheet_query varchar(10000); | |
declare not_found condition for '02000'; | |
declare at_end integer default 0; | |
declare spreadsheets cursor for | |
select ifs_path, spreadsheet_query | |
from coolstuff.spreadsheet_generator; | |
declare continue handler for sqlexception set at_end = 1; | |
declare continue handler for not_found set at_end = 1; | |
open spreadsheets; | |
fetch from spreadsheets into v_ifs_path, v_spreadsheet_query; | |
while (at_end = 0) do | |
set v_ifs_full_path = v_ifs_path concat lpad(month(current date), 2, 0) concat | |
lpad(day(current date), 2, 0) concat year(current date) concat '.xlsx'; | |
set cmdtext = | |
'STRQSH CMD(''java -Dcom.ibm.iaccess.ActLikeExternal=true -jar /home/acs/acsbundle.jar ' | |
concat '/plugin=cldownload /system=COMMON1 /clientfile=' concat v_ifs_full_path | |
concat ' /sql="' concat v_spreadsheet_query concat '"'')'; | |
-- call systools.lprintf('Speadsheet being generated: ' concat v_ifs_full_path); | |
call qsys2.qcmdexc(cmdtext); | |
set v_cmdstmt = | |
'SNDSMTPEMM RCP((''[email protected]'' *pri)) SUBJECT(''COMMON1 report:' concat | |
v_ifs_full_path concat ''') NOTE(''Common1 report ' concat | |
lpad(month(current date), 2, 0) concat lpad(day(current date), 2, 0) concat | |
year(current date) concat ''') ATTACH(''' concat v_ifs_full_path concat ''')'; | |
-- call systools.lprintf('Speadsheet being emailed: ' concat v_ifs_full_path); | |
call qsys2.qcmdexc(v_cmdstmt); | |
fetch from spreadsheets into v_ifs_path, v_spreadsheet_query; | |
end while; | |
close spreadsheets; | |
end; | |
stop; | |
call coolstuff.generate_spreadsheets(); | |
stop; | |
create or replace procedure coolstuff.email_spreadsheets () | |
begin | |
declare cmdtext clob(2k); | |
declare v_cmdstmt varchar(2000); | |
declare v_ifs_path varchar(1000); | |
declare v_ifs_full_path varchar(2000); | |
declare v_spreadsheet_query varchar(10000); | |
declare not_found condition for '02000'; | |
declare at_end integer default 0; | |
declare spreadsheets cursor for | |
select ifs_path, spreadsheet_query | |
from coolstuff.spreadsheet_generator; | |
declare continue handler for sqlexception set at_end = 1; | |
declare continue handler for not_found set at_end = 1; | |
open spreadsheets; | |
fetch from spreadsheets into v_ifs_path, v_spreadsheet_query; | |
while (at_end = 0) do | |
set v_ifs_full_path = v_ifs_path concat lpad(month(current date), 2, 0) concat | |
lpad(day(current date), 2, 0) concat year(current date) concat '.xlsx'; | |
call systools.lprintf('Speadsheet being emailed: ' concat v_ifs_full_path); | |
set v_cmdstmt = | |
'SNDSMTPEMM RCP((''[email protected]'' *pri)) SUBJECT(''COMMON1 report'') NOTE(''Common1 report ' | |
concat lpad(month(current date), 2, 0) concat | |
lpad(day(current date), 2, 0) concat year(current date) concat | |
''') ATTACH(''' concat v_ifs_full_path concat ''')'; | |
call qsys2.qcmdexc(v_cmdstmt); | |
fetch from spreadsheets into v_ifs_path, v_spreadsheet_query; | |
end while; | |
close spreadsheets; | |
end; | |
stop; | |
call coolstuff.email_spreadsheets(); | |
stop; | |
-- Create the spreadsheets on a schedule | |
cl: ADDJOBSCDE JOB(SSHEETS) CMD(RUNSQL SQL('call coolstuff.generate_spreadsheets()') COMMIT(*NONE) NAMING(*SQL)) FRQ(*WEEKLY) SCDDATE(*NONE) SCDDAY(*ALL) SCDTIME(235500) ; | |
stop; | |
-- | |
-- description: Review the setup | |
-- | |
select * | |
from qsys2.scheduled_job_info | |
where scheduled_job_name = 'SSHEETS'; | |
Hello Scott,
...
- 3 For interactive 5250 sessions an option to remove ENTER and log at the end of SYSTOOLS.GENERATE_SPREADSHEET execution
Thank you Giovanni
For Point 3 you can use the Environnement Variable ' QIBM_QSH_CMD_OUTPUT' set to 'NONE' to avoid that
Hello Scott,
...
- 3 For interactive 5250 sessions an option to remove ENTER and log at the end of SYSTOOLS.GENERATE_SPREADSHEET execution
Thank you Giovanni
For Point 3 you can use the Environnement Variable ' QIBM_QSH_CMD_OUTPUT' set to 'NONE' to avoid that
Great! Thanks!
Oh boy, this function works in mysterious ways. For posterity—and to hopefully assist a few others along the way with the Generate_Spreadsheet function and the ever-so-helpful error message:
SQLSTATE=VSP05 MESSAGE=GENERATE_SPREADSHEET: ACS - cldownload FAILED - CONSULT JOBLOG FOR DETAILS.
here's a few finding of mine.
- VSP05 Error: If you encounter a VSP05 error, note that it is not an authority error on the ACS file (authority errors are denoted as VSP06).
- Environment Variable: When troubleshooting, ensure that the environment variable
QIBM_QSH_CMD_OUTPUT
is set to'STDOUT'
. This configuration will ensure that a Java dump is created in/home/_USER_/IBM/iAccessClient/Dumps
. - JT400 Compatibility: Verify that you don't have an outdated or misplaced JT400 or similar files in
QIBM/USERDATA/JAVA400
(this one took quite some time to figure out). JT400 should in theory only be on ProdData. - User Enablement: The user running the command must be enabled and have an active password, even for scheduled job users.
- Temporary Tables: Avoid using queries on QTEMP or temporary tables, as Generate_Spreadsheet runs in a new job, which won't share the QTEMP library.
Hopefully, this will help clarify some common pitfalls and save others some troubleshooting time!
@JulienBellio / @forstie This was very helpful thank you. I had an issue where this wasn't working for some users, and it relates to their user profiles being expired. This is an issue for us as we use single signon primarily which allows you to sign on even if you password is expired, but also have a system policy to set passwords to expired after 3 months, as not all users use SSO. Something to note.
Hello everybody. We ran into a problem when using GENERATE_SPREADSHEET. Our application generally submits jobs to a single job JOBQ. When creating an Excel file with programs using this function, it crashes stating that there are no resources for starting child process. Submitting to another jobq with no maximum jobs allowed, it works.
This is the message we get (text in Italian)
/QIBM/ProdData/Java400/bin/java: 001-0012 Errore rilevato durante l'avvio del processo child. Non ci sono risorse di sistema disponi
Are there any options/system settings/environment variables to avoid this behaviour ? It would be good if the Excel creation process occurred in a system jobq (eg. QSYSNOMAX) while the SQL function waits for completion...
Thank you
Hi,
I hope you're able to increase the jobs allowed or switch to a jobq that has a different configuration.
Fwiw, I haven't had this issue reported by others.
Good list Julien!
I agree that its a frustrating service to diagnose should there be a failure.
Thanks for sharing.
Hi, I hope you're able to increase the jobs allowed or switch to a jobq that has a different configuration. Fwiw, I haven't had this issue reported by others.
Hello Scott. Finally I wrapped it up in a custom command, that submits the Excel creation to QSYSNOMAX jobq and waits for completion. All was due to our particular configurations of job queues.
Thank you,
Massimo
Hello Scott,
I have a couple of extra features/requests:
Thank you
Giovanni