piątek, 30 listopada 2012

Oracle DB 11g install failure HDD > 2TB

Setup.exe just "blinks"
You can't see whats wrong :(((

Check log file:
c:\users\Johny\AppData]\Local\Temp\Orainstall....\installActions2012-11-30_01-22-59PM.log
 
oracle 11g The number of files bootstrapped for the jre is 0.

If you see line above, check if you don't have HDD > 2TB.

Just resize your disk  :)))

czwartek, 22 listopada 2012

Uninstall OBIEE 11g



Here you go …
  • Stop all running processes for Admin and Managed servers including BI components services. Stop all the processes, if any, created during service process creation for e.g. Windows Process.
  • Remove the Weblogic Server using the Un-installation wizard under Control Panel.
  • Perform De-install from Oracle Business Intelligence -) De-install option of Program menu
  • Drop the RCU schemas using the RCU batch file -) Drop option
  • Manually remove the RCU Tablespace DBF file (to have a clean new RCU schema) . In my case I didn’t remove the DBF file as the Database server is under DBA’s control .Hence, to be safer side I re-create the RCU during the fresh installation process under different tablespace. This will make sure nothing is going to messed up with your previous installation … See below image:
  • De-install Middleware Oracle home (traverse to ..\[MIDDLEWARE_HOME]\Oracle_BI\oui\bin\ and execute below from command line: setup.exe -deinstall)
  • De-install the Middleware common Oracle Home  (traverse to ..\[MIDDLEWARE_HOME]\oracle_common\oui\bin\ and execute below from command line: setup.exe -deinstall –jreLoc  ..\[MIDDLEWARE_HOME]\Oracle_BI1\jdk)
  • Remove the Middleware Home directory with all contents forcefully,if reqd.
  • Remove the environment variable pointing to earlier locations and the relevant paths if any. This is Important!
  • Re-boot the entire server.
  • Delete the Oracle common keys , if any, from registry. This is Important ! Most of the common problem lies here . So in my case I follow the process of finding my Middleware Home path from Registry and delete all relevant Keys and values . Be careful while playing with registry :)
  • Remove the windows service process key from registry , if any
  • As in my case I don’t need to De-install my Oracle Client/DB Home hence I skip the process as I going to re-use the same Oracle Home in the next installation time.So if you are going to do the same probably you can skip De-installing your Oracle Client/DB home.
Thats it , Welldone! until you have performed the clean installa

wtorek, 13 listopada 2012

pgbouncer bin

http://www.winpg.jp/~saito/pgbouncer/


PG misc


http://vnull.pcnet.com.pl/db/psql.tricks

[From documentation]:
======================

SELECT pg_stat_get_backend_pid(S.backendid) AS procpid,pg_stat_get_backend_activity(S.backendid) AS current_query FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS S;


SELECT d.oid AS datid, d.datname, pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_userid(s.backendid) AS usesysid, u.usename, pg_stat_get_backend_activity(s.backendid) AS current_query FROM pg_database d, (SELECT pg_stat_get_backend_idset() AS backendid) s, pg_shadow u WHERE ((pg_stat_get_backend_dbid(s.backendid) = d.oid) AND (pg_stat_get_backend_userid(s.backendid) = u.usesysid));


SELECT relname, idx_tup_fetch as seeks, n_tup_ins + n_tup_upd + n_tup_del as writes FROM pg_stat_user_tables ORDER BY relname;

SELECT pid, mode, current_query FROM pg_locks, pg_stat_activity 
WHERE granted=false AND locktype = 'transactionid' AND pid=procpid ORDER BY pid, granted;


[blog www.depesz.com]:
======================

// w bajtach wielkosc bazy
select pg_database_size(db);

// czas najdluzszego wykonywanego aktualnie zapytania
select cast(extract(epoch from now() - query_start) * 1000 as int8) from pg_stat_activity;

// zwraca w bajtach wielkosc tabeli/indexu
select pg_relation_size(nazwa_relacji);

// ile transakcji (od ostatniego restartu) postgres wykonal (niezaleznie czy byly one zatwierdzone czy wycofane)
select sum(xact_commit) + sum(xact_rollback) from pg_stat_database;

// replikacja via slony 
select st_lag_num_events, cast(extract(epoch from st_lag_time) * 1000 as int8) from _slony.sl_status

[other queries]:

SELECT
    datname,
    COUNT(*) AS open_connections,
    MAX(backend_start) AS oldest_connection,
    MIN(backend_start) AS newest_connection
    FROM pg_stat_activity GROUP BY datname
UNION SELECT
    'Summary',
    COUNT(*),
    MAX(backend_start),
    MIN(backend_start)
    FROM pg_stat_activity;

[Size queries]:
===============

Databases:
SELECT datname,pg_size_pretty(pg_database_size(oid)) FROM pg_database ORDER BY pg_database_size(oid) DESC;

Tables and indexes:
SELECT relname,pg_size_pretty(pg_relation_size(oid)) FROM pg_class WHERE relname NOT LIKE 'pg_%' ORDER BY pg_relation_size(oid) DESC;

Tables only:
SELECT pg_tables.tablename, pg_tables.schemaname, pg_size_pretty(pg_relation_size((pg_tables.schemaname::text || '.'::text) || pg_tables.tablename::text)) AS pg_size_pretty, pg_relation_size((pg_tables.schemaname::text || '.'::text) || pg_tables.tablename::text) AS rs
    FROM pg_tables
   ORDER BY pg_relation_size((pg_tables.schemaname::text || '.'::text) || pg_tables.tablename::text) DESC;

Indexes only:
SELECT pg_indexes.indexname, pg_size_pretty(pg_relation_size((pg_indexes.schemaname::text || '.'::text) || pg_indexes.indexname::text)) AS pg_size_pretty, pg_relation_size((pg_indexes.schemaname::text || '.'::text) || pg_indexes.indexname::text) AS rs
    FROM pg_indexes
   ORDER BY pg_relation_size((pg_indexes.schemaname::text || '.'::text) || pg_indexes.indexname::text) DESC;

Introspection
If there's a relation with name FOO
SELECT COUNT(relname) FROM pg_class WHERE relname = 'FOO';


[Env]:
======
export PGDATA=path/to/cluster
export PGPORT=5432
export PGHOST=/tmp_or_path_to_".pgsql"_socket


[Utils]:
========
ptop
PgBouncer: https://developer.skype.com/SkypeGarage/DbProjects/PgBouncer


[Parameters]
shared_buffers - 10-25% RAM (15%!)
fullpage_writes ?? = raid cache protected by batteries(?!)
log_min_duration_statement.
log_timestamp = true

http://www.westnet.com/~gsmith/content/postgresql/
http://www.westnet.com/~gsmith/content/postgresql/chkp-bgw-83.htm

http://notemagnet.blogspot.com/2008/08/linux-write-cache-mystery.html
http://www.westnet.com/~gsmith/content/linux-pdflush.htm

http://www.kaltenbrunner.cc/blog/index.php?/archives/21-guid.html

http://blog.charcoalphile.com/category/postgresql/

IO contention discussed:
http://qaix.com/postgresql-database-development/337-722-tuning-postgres-for-large-data-import-using-copy-from-read.shtml
http://archives.postgresql.org/pgsql-performance/2005-08/msg00144.php
http://www.mail-archive.com/pgsql-performance@postgresql.org/msg18102.html


wylaczac foreign key i indeksy przy loadzie !
COPY dump
ip4r type

# 50 % RAM
effective_cache_size =

# jesli czescniej niz x sekund
# 2..5 minut?
checkpoint_warning = 300


reduced cpu_index_tuple_cost to 0.0005
(encourages indexes which may reduce disk hits)

increased work_mem to 15000 - sort/create index(sort!)

"commit=600" and writeback for ext3

"This tunable is used to define when dirty data is old enough to be eligible for writeout by the pdflush daemons. It is expressed in 100'ths of a second. Data which has been dirty in memory for longer than this interval will be written out next time a pdflush daemon wakes up.":
echo 60000 > /proc/sys/vm/dirty_expire_centisecs


alter table set statistics

"The default settings for autovacuum are very conservative, though, and are more suitable for a very small database. I generally use something aggressive like:
-D -v 400 -V 0.4 -a 100 -A 0.3
This vacuums tables after 400 rows + 40% of the table has been updated or deleted, and analyzes after 100 rows + 30% of the table has been inserted, updated or deleted. The above configuration also lets me set my max_fsm_pages to 50% of the data pages in the database with confidence that that number won't be overrun, causing database bloat. We are currently testing various settings at OSDL and will have more hard figures on the above soon."


max_fsm_relations = ilosc tabel w pgsql + zapas
max_fsm_pages = minimum na tyle ile wynosi suma liczb stron wywietlanych przez VACUUM
VERBOSE dla wszystkich tabel. 

0 3 * * * root psql -c 'VACUUM FULL;' test
0 3 * * * root vacuumdb -a -f


SELECT relfilenode, relpages * 8 AS kilobytes FROM pg_class ORDER BY relpages DESC;

SELECT c2.relname, c2.relpages * 8 AS kilobytes
FROM pg_class c, pg_class c2, pg_index i
WHERE
c.oid = i.indrelid AND
c2.oid = i.indexrelid
ORDER BY c2.relname;

FSM:
VACUUM ANALYZE VERBOSE ;

CLUSTER - reorganizuje tabele na bazie indeksu (alfanumeric sort, dane blisko siebie w przypadku index range scanu)

http://www.wlug.org.nz/PostgreSQLNotes
commit_delay = 20000
commit_siblings = 3

wal_buffers = 128
checkpoint_timeout = 600
checkpoint_warning = 300
checkpoint_segments = (wal_write_rate * 600) / 16
checkpoint_segments is (300 * 3) / 16 = 56
For example, to complete recovery within 5 minutes at 3.0MiB/sec

http://www.postgresonline.com/journal/index.php?/archives/10-How-does-CLUSTER-ON-improve-index-performance.html

"It is impossible to tune checkpoint_segments for worst-case WAL activity, such as a bulk load using COPY FROM STDIN SQL command. This might generate 30MiB/s or more of WAL activity. 30MiB/s for 5 minutes is 8.7GiB of WAL data! The best suggestion appears to be let the server run for a while, then study the timestamps on the WAL segment files in pg_xlog to calculate the WAL write rate."

COPY is fastest when used within the same transaction as an earlier CREATE TABLE or TRUNCATE command. In such cases no WAL needs to be written, because in case of an error, the files containing the newly loaded data will be removed anyway. However, this consideration does not apply when archive_mode is set, as all commands must write WAL in that case.

Temporary increasing the maintenance_work_mem configuration variable when loading large amounts of data can lead to improved performance. This will help to speed up CREATE INDEX commands and ALTER TABLE ADD FOREIGN KEY commands. It won't do much for COPY itself, so this advice is only useful when you are using one or both of the above techniques.

First for fairly static tables such as large lookup tables, that rarely change or when they change are bulk changes, there is little point in leaving blank space in pages. It takes up disk space and causes Postgres to scan thru useless air. In these cases - you basically want to set your FillFactor high to like 99.

CREATE UNIQUE INDEX (!)

osobny tablespace indextblspace

\set ECHO_HIDDEN t


2. Give the file system a hint that you work with larger block sizes.
   Ext3: mke2fs -b 4096 -j -R stride=2 /dev/sda1 -L LABEL
   I made a I/O test with PostgreSQL on a RAID system with stripe size
   of 64kByte and block size of 8 kByte in the RAID system.
   Stride=2 was the best value. 

64/4 = 16

largefile!

For example if you have a 4 drive raid 5 and it is using 64K chunks, your stripe size will be 256K. Given a 4K filesystem block size you would then have a stride of 64 (256/4). 
If it was 4 disk RAID0 array, than it would be 64(4x64k/4k=64). 
If it was 4 disk RAID10 array, than it would be 32 ((4/2)*64k/4k=32)
4 dyski przez 2 (RAID10) = 2 * 64kB (stripe-size) / 4kB (fs-block-size) === 32 dla 64kB chunk size


-------------8<-----------_ -------------8="-------------8" -ald="-ald" -d="-d" -l="-l" -m="-m" -s="-s" -sh="-sh" 0.69="0.69" 00000001.history:="00000001.history:" 0000000100000006000000f3.00000068.backup:="0000000100000006000000f3.00000068.backup:" 0000000100000006000000f3:="0000000100000006000000f3:" 0000000100000006000000f3="0000000100000006000000f3" 0="0" 1.2g="1.2g" 1.95="1.95" 11="11" 12:56:51="12:56:51" 12:56:53="12:56:53" 12:58:45="12:58:45" 12="12" 13:00:28="13:00:28" 13:00:33="13:00:33" 13:01:58="13:01:58" 13:01:59="13:01:59" 13:02:00="13:02:00" 13:02:01="13:02:01" 13:02:02="13:02:02" 13:02="13:02" 1470="1470" 15:15:37="15:15:37" 15:15="15:15" 15:17:07="15:17:07" 16="56" 1="1" 2.="2." 2009-01-15="2009-01-15" 2009-01-16="2009-01-16" 210="210" 2205="2205" 24990="24990" 25072="25072" 29006="29006" 3.0mib="3.0mib" 3.="3." 3157="3157" 3910="3910" 3917="3917" 3="3" 4.="4." 4096="4096" 438896="438896" 458752="458752" 4710="4710" 4734="4734" 4738="4738" 4744="4744" 5.="5." 5="5" 6.="6." 6="6" 7.="7." 7656="7656" 8932="8932" 8a="8a" 8b="8b" 95.67="95.67" a.granted="false" a.pid="c.procpid;" a.relation::regclass="a.relation::regclass" a.relation="b.relation" a.transactionid="a.transactionid" a="a" above="above" activity="activity" an="an" and="and" archives.postgresql.org="archives.postgresql.org" archives="archives" archiving="archiving" as="as" at="at" attempting="attempting" available="available" awk="awk" b.granted="true" b.pid="b.pid" b="b" backup="backup" be:="be:" be="be" beat="beat" below="below" best.="best." bin="bin" blah="blah" bloat="bloat" boot="boot" both="both" box:="box:" box="box" browser="browser" but="but" by="by" c.oid="i.indrelid" c.usename="c.usename" c2.relname="c2.relname" c2="c2" c="c" cache:="cache:" cache="cache" caching="caching" called="called" can="can" check="check" checking="checking" checkpoint_segments="checkpoint_segments" complete="complete" conenctivity:="conenctivity:" config="config" configured="configured" configuring="configuring" connection="connection" contained="contained" contrib="contrib" copy="copy" correct="correct" cos:="================================================================" cos_="cos_" count:="count:" data.master="data.master" data="data" database.="database." database="database" db.postgresql.skytools.user="db.postgresql.skytools.user" dbname="template1]" dead="dead" dead_tuple_count="dead_tuple_count" dead_tuple_len="dead_tuple_len" dead_tuple_percent="dead_tuple_percent" definicji="definicji" determine="determine" directory="directory" disabling="disabling" done="done" down....="down...." drwx------="drwx------" dtrace="dtrace" du="du" dump="dump" each="each" enables="enables" entirely="entirely" example="example" exclusive="exclusive" execute="execute" exists="exists" exiting="exiting" fast="fast" fatal:="fatal:" file:="file:" file="file" first="first" for="for" found="found" free_percent="free_percent" free_space="free_space" from="from" full="full" function="function" funkcji="funkcji" gb="gb" generates="generates" good="good" got="got" gotta="gotta" ha="ha" have="have" heap="heap" heartbeat="heartbeat" help="help" home="home" http:="http:" i.indexrelid="c2.oid" i.indisprimary="f" i="i" ibm1:="ibm1:" ibm1="ibm1" ibm2:="ibm2:" ignoring="ignoring" impact="impact" in="in" index.="index." index.php="index.php" index="index" indexow="indexow" info.="info." info="info" io="io" is:="is:" is="is" it.="it." it="it" keep="keep" keepalived="keepalived" labs.omniti.com="labs.omniti.com" length="length" linux:="linux:" lock="lock" lots="lots" ls="ls" maintenance="maintenance" makarevitch.org="makarevitch.org" mammoth="mammoth" master="master" maximum="maximum" may="may" message="message" minutes="minutes" ml="ml" mode="mode" module="module" more="more" move="move" msg00089.html="msg00089.html" msg00274.php="msg00274.php" must="must" na="na" necessary="necessary" new="new" normal="normal" not.="not." not="not" obtained.="obtained." of="of" old-slave="old-slave" old="old" on="on" one="one" or="or" order="order" osdir.com="osdir.com" other="other" over="over" particular="particular" passes="passes" past="past" patch="=======" people.planetpostgresql.org="people.planetpostgresql.org" percentage="percentage" performance="performance" pg_catalog.pg_class="pg_catalog.pg_class" pg_catalog.pg_get_indexdef="pg_catalog.pg_get_indexdef" pg_catalog.pg_index="pg_catalog.pg_index" pg_catalog.pg_proc="pg_catalog.pg_proc" pg_ctl="pg_ctl" pg_dump="pg_dump" pg_hba.conf="pg_hba.conf" pg_ident.conf="pg_ident.conf" pg_locks="pg_locks" pg_okazjerw.0="pg_okazjerw.0" pg_okazjerw="pg_okazjerw" pg_start_backup="pg_start_backup" pg_stat_activity="pg_stat_activity" pgdbarw="pgdbarw" pgsql-performance="pgsql-performance" pgstattuple="pgstattuple" physically="physically" pid_locked="pid_locked" pid_locker="pid_locker" pidfile="pidfile" postgresql-database-development="postgresql-database-development" postgresql.conf="postgresql.conf" postgresql="postgresql" postmaster.pid="postmaster.pid" postmaster:="postmaster:" postmaster="postmaster" pplications="pplications" pre="pre" print="print" probes="probes" problem-with-pqsendquery-pqgetresult-and-copy-from-statement-read.shtml="========" production="production" project-dtrace="project-dtrace" properly="=======" psql:="psql:" psql="psql" qaix.com="qaix.com" raid="raid" ram="ram" random_page_cost="0.5" rant="rant" recovery.conf="recovery.conf" recovery="recovery" regex="regex" regular="regular" released.="released." renamed="renamed" replicator="replicator" required="required" requires="requires" restore="restore" returns="returns" running="running" scenario="scenario" sec="sec" see="see" select="select" sending="sending" seq_page_cost="0.3" server="server" serverlog="serverlog" setting="setting" setup="setup" shouldn="shouldn" shrink="shrink" shut="shut" sighup="sighup" significant="significant" significantly="significantly" size="size" slave="slave" sql:="sql:" start="start" starting="starting" stop="stop" stopped="stopped" stopping="stopping" stored="stored" successful="successful" such="such" syncdaemon.="syncdaemon." system="system" systemexit="systemexit" t="t" tabeli="tabeli" table="table" table_len="table_len" tablespaces="tablespaces" tail="tail" test="test" that="that" the="the" this="this" to="to" tool="tool" trac="trac" true="true" trunk="trunk" tunning="tunning" tuple_count="tuple_count" tuple_len="tuple_len" tuple_percent="tuple_percent" tuples="tuples" two="two" ullbackup="ullbackup" up="up" usamadar="usamadar" used="used" useful="useful" user_locked="user_locked" users="users" using="using" usr="usr" utovacuum-internals.html="utovacuum-internals.html" vacuum="vacuum" verify="verify" very="very" waiting="waiting" wal-master.ini="wal-master.ini" wal-slave.ini="wal-slave.ini" wal-slave.log="wal-slave.log" wal="wal" walmgr.py="walmgr.py" walmgr="walmgr" warning="warning" way="way" we="we" well="well" where="where" whether="whether" which="which" wiki="wiki" within="within" working:="working:" write="write" wszystkich="wszystkich" wyciagniecie="wyciagniecie" z="z">

piątek, 2 listopada 2012

Windows TCP source port + PID , DLL

Link1
SRC_PORT + PID

------
;~  _GetTCPtable( [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll" ] ] ) ;~ ;~  Return Value ;~  Success:    TCPtable[][] = 2-D array ;~               [0][0] = number of connections ;~           for connection n: ;~               [n][0] = connection state (integer) ;~               [n][1] = local IP ;~               [n][2] = local port ;~               [n][3] = remote IP ;~               [n][4] = remote port ;~               [n][5] = connection state (informative text) ;~  Failure:    TCPtable[0][0] = -1 Func _GetTCPtable($WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")     Local Const $connState[12] = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", _             "FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]         Local $TCPtable[1][1] = [[ -1]] ; preset to "failed"     $dwSize = DllStructCreate("dword") ; for MIB_TCPTABLE buffer size     $MIB_TCPTABLE = DllStructCreate("dword") ; nominal struct initially     DllStructSetData($dwSize, 1, 0) ; force zero size     $ret = DllCall($IHdll, "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get size     If @error Or $ret[0] <> 122 Then Return $TCPtable ; dllCall error or RC is *not* ERROR_INSUFFICIENT_BUFFER = 122     $MIB_TCPTABLE = ""     For $i = 1 To DllStructGetData($dwSize, 1) / 4 ; make to requested size of buffer         $MIB_TCPTABLE &= "dword;"     Next     $MIB_TCPTABLE = DllStructCreate(StringTrimRight($MIB_TCPTABLE, 1)) ; requested struct     DllStructSetData($dwSize, 1, DllStructGetSize($MIB_TCPTABLE)) ; recheck its size     $ret = DllCall($IHdll, "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get data     If @error Or $ret[0] Then Return $TCPtable ; dllCall error or RC is Error     $numTCPentries = DllStructGetData($MIB_TCPTABLE, 1) ; number of entries     ReDim $TCPtable[$numTCPentries + 1][6]         For $i = 1 To $numTCPentries         $offset = ($i - 1) * 5 + 1 ; dword offset into struct         $TCPtable[$i][0] = DllStructGetData($MIB_TCPTABLE, $offset + 1) ; integer connection state         $TCPtable[$i][5] = $connState[$TCPtable[$i][0] - 1] ; connection state text                 $ret = DllCall($WSdll, "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 2)) ; local IP / translate         If @error Then Return $TCPtable ; dllCall error         $TCPtable[$i][1] = $ret[0]         $ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 3)) ; local port / translate         If @error Then Return $TCPtable ; dllCall error         $TCPtable[$i][2] = $ret[0]                 If $TCPtable[$i][0] <= 2 Then ; CLOSED or LISTENING state             $TCPtable[$i][3] = "0.0.0.0"             $TCPtable[$i][4] = 0         Else             $ret = DllCall($WSdll, "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 4)) ; remote IP / translate             If @error Then Return $TCPtable ; dllCall error             $TCPtable[$i][3] = $ret[0]             $ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 5)) ; remote port / translate             If @error Then Return $TCPtable ; dllCall error             $TCPtable[$i][4] = $ret[0]         EndIf     Next     $dwSize = 0     $MIB_TCPTABLE = 0     $TCPtable[0][0] = $numTCPentries ; success     Return $TCPtable     EndFunc   ;==>_GetTCPtable ;~  _CloseTCPconnection( LocalIP, LocalPort, RemoteIP, RemotePort, [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll"]] ) ;~ ;~  Return Value ;~  Success:    1 ;~  Failure:    0 Func _CloseTCPconnection($localIP, $localPort, $remoteIP, $remotePort, $WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")     $MIB_TCPROW = DllStructCreate("dword;dword;dword;dword;dword") ; connection struct     DllStructSetData($MIB_TCPROW, 1, 12) ; set to DELETE_TCB state = 12         $ret = DllCall($WSdll, "uint", "inet_addr", "str", $localIP) ; local IP / translate     If Not @error Then DllStructSetData($MIB_TCPROW, 2, $ret[0])     $ret = DllCall($WSdll, "uint", "htons", "ushort", $localPort) ; local port / translate     If Not @error Then DllStructSetData($MIB_TCPROW, 3, $ret[0])     $ret = DllCall($WSdll, "uint", "inet_addr", "str", $remoteIP) ; remote IP / translate     If Not @error Then DllStructSetData($MIB_TCPROW, 4, $ret[0])     $ret = DllCall($WSdll, "uint", "htons", "ushort", $remotePort) ; remote port / translate     If Not @error Then DllStructSetData($MIB_TCPROW, 5, $ret[0])     $ret = DllCall($IHdll, "int", "SetTcpEntry", "ptr", DllStructGetPtr($MIB_TCPROW)) ; close connection     If @error Or $ret[0] Then Return 0 ; dllCall error or RC is Error         $MIB_TCPROW = 0     Return 1 ; success     EndFunc   ;==>_CloseTCPconnection
-----------------


---------------------


Alright, so I'm extending my company's flexlm vendor daemon to be a little bit more revealing to client applications.
I need to be able to find out what port lmgrd is listening on before clients connect. The API documentation seems to be rather barren, and I believe they keep most of their code in a compiled form so I can't just look at their source.
Is it possible to call upon the awesome power of the Windows API to find out what ports a particular process is using? If Process Explorer from sysinternals can do it, I should be able to right? Does anyone have sample code for this?
It needs to support Windows XP and higher since many of our clients have yet to upgrade.
Edit: I should note that it turns out FLEX has support for pulling the port from the license file. I don't have the code in front of me, but know that this isn't the best way to find out what ports your vendor daemon/lmgrd is running.
share|improve this question

64% accept rate
feedback

3 Answers

GetTcpTable2 -- see EDIT
The GetTcpTable function retrieves the IPv4 TCP connection table.
This will fill in a MIB_TCPTABLE structure.
typedef struct _MIB_TCPTABLE {
  DWORD      dwNumEntries;
  MIB_TCPROW table[ANY_SIZE];
} MIB_TCPTABLE, *PMIB_TCPTABLE;
And now the MIB_TCPROW
typedef struct _MIB_TCPROW {
  DWORD dwState;
  DWORD dwLocalAddr;
  DWORD dwLocalPort;
  DWORD dwRemoteAddr;
  DWORD dwRemotePort;
} MIB_TCPROW, *PMIB_TCPROW;
EDIT IMPORTANT:
You need to use GetTcpTable2 in order to get the corresponding PID associated as well.
typedef struct _MIB_TCPROW2 {
  DWORD                        dwState;
  DWORD                        dwLocalAddr;
  DWORD                        dwLocalPort;
  DWORD                        dwRemoteAddr;
  DWORD                        dwRemotePort;
  DWORD                        dwOwningPid;
  TCP_CONNECTION_OFFLOAD_STATE dwOffloadState;
} MIB_TCPROW2, *PMIB_TCPROW2;
dwOwningPid
share|improve this answer
Crap. No can do. Seems the GetTcpTable2 function is a Vista and higher function. I should have specified it should work with Windows XP as a minimum since many of our clients haven't upgraded yet. I'll update my original post. Excellent find though, I must say. – RandomInsano Jun 28 '11 at 20:14
So... it seems the closest Windows XP version of this is AllocateAndGetTcpExTableFromStackmsdn.microsoft.com/en-us/library/aa365804(v=vs.85).aspx – RandomInsano Jun 28 '11 at 20:57
1 
The GetTcpTable or GetExtendedTcpTable functions should be used to retrieve the TCP connection table instead of using the AllocateAndGetTcpExTableFromStack function. Note The AllocateAndGetTcpExTableFromStack function is deprecated and not supported on Windows Vista and later. On the Microsoft Windows Software Development Kit (SDK) released for Windows Vista and later, the function prototype for AllocateAndGetTcpExTableFromStack is still defined in the Iphlpapi.h header file for continued support on Windows Server 2003 and Windows XP. – kisplit Jun 28 '11 at 21:02
1 
Use: GetExtendedTcpTable and GetOwnerModuleFromTcpEntry – kisplit Jun 28 '11 at 21:02
Just found the GetExtendedTcpTable. Awesome! – RandomInsano Jun 28 '11 at 21:16
feedback
In the worst case, you could always parse the output of:
netstat -bna
share|improve this answer
Interesting idea. More to find a reverse engineered version of that app. I've fallen upon the old winternals source code and will see how that fellow implemented his TCPView application. – RandomInsano Jun 28 '11 at 20:30
feedback
Here's the code I ended up with, for anyone who hits this problem after me
#include "stdafx.h"
#include 
#include 

// These are just for the ntohl function in the printf below
#include 
#pragma comment(lib, "Ws2_32.lib")

DWORD (WINAPI *pGetExtendedTcpTable)(
  PVOID pTcpTable,
  PDWORD pdwSize,
  BOOL bOrder,
  ULONG ulAf,
  TCP_TABLE_CLASS TableClass,
  ULONG Reserved
);

int _tmain(int argc, _TCHAR* argv[])
{
    MIB_TCPTABLE_OWNER_PID *pTCPInfo;
    MIB_TCPROW_OWNER_PID *owner;
    DWORD size;
    DWORD dwResult;

    HMODULE hLib = LoadLibrary( "iphlpapi.dll" );

    pGetExtendedTcpTable = (DWORD (WINAPI *)(PVOID,PDWORD,BOOL,ULONG,TCP_TABLE_CLASS,ULONG))
        GetProcAddress(hLib, "GetExtendedTcpTable");

    if ( !pGetExtendedTcpTable )
    {
        printf("Could not load iphlpapi.dll. This application is for Windows XP SP2 and up.\n");
        return 1;
    }

    dwResult = pGetExtendedTcpTable(NULL,       &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0);
    pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)malloc(size);
    dwResult = pGetExtendedTcpTable(pTCPInfo,   &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0);

    if (dwResult != NO_ERROR)
    {
        printf("Couldn't get our IP table");
        return 2;
    }

    printf("Iterating though table:\n");
    for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
    {
        owner = &pTCPInfo->table[dwLoop];

        printf("  PID: %5u - Port: %5u\n", owner->dwOwningPid, ntohs(owner->dwLocalPort));
    }

    // Pause a moment
    printf("Done Processing\n");

    return 0;
}
share|improve this answer

Ginekolog dr n. med. Piotr Siwek

Gabinet ginekologiczny specjalista ginekolog - położnik dr n. med. Piotr Siwek